Generate a matrix from a given string - python

I need to create a matrix from a string s where m is the given number of rows and len(s)/m is the number of columns. First column must be filled with the first m chars in the string s (I.E.: 0*m+i chars for every i in range(m) ); the second column with the 1*m+i and so on.
What's the best way to do this in python?
EDIT:
this is the code I wrote by now.
def split_by_n( seq, n ):
"""A generator to divide a sequence into chunks of n units."""
while seq:
yield seq[:n]
seq = seq[n:]
#print list(split_by_n("1234567890",2))
input=list("ZPFKYLGJPNSGNMQGFGCITLVRIWMGFBLBFDSIOAJGBGAVFVHBGLFSRPNIOFSYOBTFCGRQLWWZAAJFUPGAFZSNXLTGARUVFKOLGAIWGUUCMVSEKLIAGJGGUZFBAOILVRIZPORNXWVFRGNMEGCEUNUZSPNIUAHFRQLWALHWEQGQKDFDCCKLUZWFSITKWIKLSMUQKNJUWRTKZAHJGABKDEGEMNCVIMBFRNYXSSKYPWLWHUKKISHFAJPOOFGJBJTBXXSGTRYAJGBNRMYHOGXQBLSFEWVUCHRLEJWAQBIWFRLWSSKRKSBFRAKDFJVRGZUOCJUZEKWAPIQSBRYM")
l = list(split_by_n(input,6))
for i in range(len(l[-2])-len(l[-1])):
l[-1].append('$')
print l

I learned from your comment that you want to make transpose of your matrix that is formed from a given string. Your code creates the matrix from a given string just fine. I have tweaked your code only slightly, and added code for making transpose.
def split_by_n( seq, n ):
while seq:
yield seq[:n]
seq = seq[n:]
def make_matrix(string):
col_count = 6
matrix = list(split_by_n(string,6))
row_count = len(matrix)
# the last row has "less_by" fewer elements than the rest of the rows
less_by = len(matrix[-2]) - len(matrix[-1])
matrix[-1] += '$' * less_by
return matrix
def make_transpose(matrix):
col_count = len(matrix[0])
transpose = []
for i in range(col_count):
transpose.append([row[i] for row in matrix])
return transpose
string = list("ZPFKYLGJPNSGNMQGFGCITLVRIWMGFBLBFDSIOAJGBGAVFVHBGLFSRPNIOFSYOBTFCGRQLWWZAAJFUPGAFZSNXLTGARUVFKOLGAIWGUUCMVSEKLIAGJGGUZFBAOILVRIZPORNXWVFRGNMEGCEUNUZSPNIUAHFRQLWALHWEQGQKDFDCCKLUZWFSITKWIKLSMUQKNJUWRTKZAHJGABKDEGEMNCVIMBFRNYXSSKYPWLWHUKKISHFAJPOOFGJBJTBXXSGTRYAJGBNRMYHOGXQBLSFEWVUCHRLEJWAQBIWFRLWSSKRKSBFRAKDFJVRGZUOCJUZEKWAPIQSBRYM")
matrix = make_matrix(string)
transpose = make_transpose(matrix)
for e in matrix:
print(e)
print('\nThe transpose:')
for e in transpose:
print(e)
Output:
['Z', 'P', 'F', 'K', 'Y', 'L']
['G', 'J', 'P', 'N', 'S', 'G']
['N', 'M', 'Q', 'G', 'F', 'G']
['C', 'I', 'T', 'L', 'V', 'R']
['I', 'W', 'M', 'G', 'F', 'B']
['L', 'B', 'F', 'D', 'S', 'I']
['O', 'A', 'J', 'G', 'B', 'G']
['A', 'V', 'F', 'V', 'H', 'B']
['G', 'L', 'F', 'S', 'R', 'P']
['N', 'I', 'O', 'F', 'S', 'Y']
['O', 'B', 'T', 'F', 'C', 'G']
['R', 'Q', 'L', 'W', 'W', 'Z']
['A', 'A', 'J', 'F', 'U', 'P']
['G', 'A', 'F', 'Z', 'S', 'N']
['X', 'L', 'T', 'G', 'A', 'R']
['U', 'V', 'F', 'K', 'O', 'L']
['G', 'A', 'I', 'W', 'G', 'U']
['U', 'C', 'M', 'V', 'S', 'E']
['K', 'L', 'I', 'A', 'G', 'J']
['G', 'G', 'U', 'Z', 'F', 'B']
['A', 'O', 'I', 'L', 'V', 'R']
['I', 'Z', 'P', 'O', 'R', 'N']
['X', 'W', 'V', 'F', 'R', 'G']
['N', 'M', 'E', 'G', 'C', 'E']
['U', 'N', 'U', 'Z', 'S', 'P']
['N', 'I', 'U', 'A', 'H', 'F']
['R', 'Q', 'L', 'W', 'A', 'L']
['H', 'W', 'E', 'Q', 'G', 'Q']
['K', 'D', 'F', 'D', 'C', 'C']
['K', 'L', 'U', 'Z', 'W', 'F']
['S', 'I', 'T', 'K', 'W', 'I']
['K', 'L', 'S', 'M', 'U', 'Q']
['K', 'N', 'J', 'U', 'W', 'R']
['T', 'K', 'Z', 'A', 'H', 'J']
['G', 'A', 'B', 'K', 'D', 'E']
['G', 'E', 'M', 'N', 'C', 'V']
['I', 'M', 'B', 'F', 'R', 'N']
['Y', 'X', 'S', 'S', 'K', 'Y']
['P', 'W', 'L', 'W', 'H', 'U']
['K', 'K', 'I', 'S', 'H', 'F']
['A', 'J', 'P', 'O', 'O', 'F']
['G', 'J', 'B', 'J', 'T', 'B']
['X', 'X', 'S', 'G', 'T', 'R']
['Y', 'A', 'J', 'G', 'B', 'N']
['R', 'M', 'Y', 'H', 'O', 'G']
['X', 'Q', 'B', 'L', 'S', 'F']
['E', 'W', 'V', 'U', 'C', 'H']
['R', 'L', 'E', 'J', 'W', 'A']
['Q', 'B', 'I', 'W', 'F', 'R']
['L', 'W', 'S', 'S', 'K', 'R']
['K', 'S', 'B', 'F', 'R', 'A']
['K', 'D', 'F', 'J', 'V', 'R']
['G', 'Z', 'U', 'O', 'C', 'J']
['U', 'Z', 'E', 'K', 'W', 'A']
['P', 'I', 'Q', 'S', 'B', 'R']
['Y', 'M', '$', '$', '$', '$']
The transpose:
['Z', 'G', 'N', 'C', 'I', 'L', 'O', 'A', 'G', 'N', 'O', 'R', 'A', 'G', 'X', 'U', 'G', 'U', 'K', 'G', 'A', 'I', 'X', 'N', 'U', 'N', 'R', 'H', 'K', 'K', 'S', 'K', 'K', 'T', 'G', 'G', 'I', 'Y', 'P', 'K', 'A', 'G', 'X', 'Y', 'R', 'X', 'E', 'R', 'Q', 'L', 'K', 'K', 'G', 'U', 'P', 'Y']
['P', 'J', 'M', 'I', 'W', 'B', 'A', 'V', 'L', 'I', 'B', 'Q', 'A', 'A', 'L', 'V', 'A', 'C', 'L', 'G', 'O', 'Z', 'W', 'M', 'N', 'I', 'Q', 'W', 'D', 'L', 'I', 'L', 'N', 'K', 'A', 'E', 'M', 'X', 'W', 'K', 'J', 'J', 'X', 'A', 'M', 'Q', 'W', 'L', 'B', 'W', 'S', 'D', 'Z', 'Z', 'I', 'M']
['F', 'P', 'Q', 'T', 'M', 'F', 'J', 'F', 'F', 'O', 'T', 'L', 'J', 'F', 'T', 'F', 'I', 'M', 'I', 'U', 'I', 'P', 'V', 'E', 'U', 'U', 'L', 'E', 'F', 'U', 'T', 'S', 'J', 'Z', 'B', 'M', 'B', 'S', 'L', 'I', 'P', 'B', 'S', 'J', 'Y', 'B', 'V', 'E', 'I', 'S', 'B', 'F', 'U', 'E', 'Q', '$']
['K', 'N', 'G', 'L', 'G', 'D', 'G', 'V', 'S', 'F', 'F', 'W', 'F', 'Z', 'G', 'K', 'W', 'V', 'A', 'Z', 'L', 'O', 'F', 'G', 'Z', 'A', 'W', 'Q', 'D', 'Z', 'K', 'M', 'U', 'A', 'K', 'N', 'F', 'S', 'W', 'S', 'O', 'J', 'G', 'G', 'H', 'L', 'U', 'J', 'W', 'S', 'F', 'J', 'O', 'K', 'S', '$']
['Y', 'S', 'F', 'V', 'F', 'S', 'B', 'H', 'R', 'S', 'C', 'W', 'U', 'S', 'A', 'O', 'G', 'S', 'G', 'F', 'V', 'R', 'R', 'C', 'S', 'H', 'A', 'G', 'C', 'W', 'W', 'U', 'W', 'H', 'D', 'C', 'R', 'K', 'H', 'H', 'O', 'T', 'T', 'B', 'O', 'S', 'C', 'W', 'F', 'K', 'R', 'V', 'C', 'W', 'B', '$']
['L', 'G', 'G', 'R', 'B', 'I', 'G', 'B', 'P', 'Y', 'G', 'Z', 'P', 'N', 'R', 'L', 'U', 'E', 'J', 'B', 'R', 'N', 'G', 'E', 'P', 'F', 'L', 'Q', 'C', 'F', 'I', 'Q', 'R', 'J', 'E', 'V', 'N', 'Y', 'U', 'F', 'F', 'B', 'R', 'N', 'G', 'F', 'H', 'A', 'R', 'R', 'A', 'R', 'J', 'A', 'R', '$']

Related

how to return a list to its original state in python?

every time a user inputs a message rotorI.append(rotorI.pop(0)) executes on the edited list, i want it to execute on the original list.
rotorI = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T','O', 'W','Y','H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J']
while True:
msg = input("Enter a msg: ")
for i in range(len(msg)):
rotorI.append(rotorI.pop(0))
print(rotorI)
I want the output to be:
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
however this the output i receive:
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K', 'M', 'F']
Use copy of your list with using copy()function:
your_main_list = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T','O', 'W','Y','H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J']
while True:
msg = input("Enter a msg: ")
list_for_edit = your_main_list.copy()
for i in range(len(msg)):
list_for_edit.append(list_for_edit.pop(0))
print(list_for_edit)
Output is gonna be :
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y',
'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y',
'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg:
Make a copy of the list at each iteration
while True:
msg = input("Enter a msg: ")
newRotor = list(rotorI)
for i in range(len(msg)):
newRotor.append(newRotor.pop(0))
print(newRotor)
Using collections.deque and string for displaying
from collections import deque
rotorI = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
while True:
msg = input("Enter a msg: ")
newRotor = deque(rotorI)
newRotor.rotate(-len(msg))
print("".join(newRotor))
Enter a msg: hi
MFLGDQVZNTOWYHXUSPAIBRCJEK
Enter a msg: hih
FLGDQVZNTOWYHXUSPAIBRCJEKM
Enter a msg: hihi
LGDQVZNTOWYHXUSPAIBRCJEKMF
Enter a msg: hi
MFLGDQVZNTOWYHXUSPAIBRCJEK

Python [x::y] slice operator - why doesn't work for me?

I have a list like this:
residL=['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A']
Desired output:
residL = ['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A\n10', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H\n20', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G\n30', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L\n40', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A\n50']
I can get this output with this piece of code:
for i in range(9,len(residL), 10):
residL[i] = '%s\n%i'%(residL[i], i+1)
But I wanted to go fancy, so I tried the slice operator:
residL[9::10] = [x+'\n%i'%(residL.index(x)+1) for x in residL[9::10]]
I got a strange result though:
residL = ['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A\n10', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H\n20', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G\n7', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L\n5', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A\n10']
I'm wondering how could it be fixed. Just for the sake of learning. :)
index is finding an earlier appearance of the same letter. Instead, use enumerate to track the index yourself.
residL=['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A']
residL[9::10] = [x+'\n%i'%((i+1)*10) for i, x in enumerate(residL[9::10])]
residL
# => ['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A\n10', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H\n20', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G\n30', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L\n40', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A\n50']
Use enumerate to keep track of index
>>> [x if (i-9)%10 else x+f'\n{i+1}' for i,x in enumerate(residL)]
['M', 'P', 'P', 'M', 'L', 'S', 'G', 'L', 'L', 'A\n10', 'R', 'L', 'V', 'K', 'L', 'L', 'L', 'G', 'R', 'H\n20', 'G', 'S', 'A', 'L', 'H', 'W', 'R', 'A', 'A', 'G\n30', 'A', 'A', 'T', 'V', 'L', 'L', 'V', 'I', 'V', 'L\n40', 'L', 'A', 'G', 'S', 'Y', 'L', 'A', 'V', 'L', 'A\n50']

Select n items by sequence of a list repeatedly in python

Say I have a long list:
>>> import string
>>> my_list = list(string.ascii_lowercase)
>>> my_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
I want to loop over this list and select n items by sequence repeatedly. E.g. if I want to select 5 items, then it should be like:
step 1: ['a', 'b', 'c', 'd', 'e']
step 2: ['f', 'g', 'h', 'i', 'j']
step 3: ['k', 'l', 'm', 'n', 'o']
step 4: ['p', 'q', 'r', 's', 't']
step 5: ['u', 'v', 'w', 'x', 'y']
step 6: ['z', 'a', 'b', 'c', 'd']
step 7: ['e', 'f', 'g', 'h', 'i']
......
So the point is: I want to make sure when I reach the last item of the list, the first items can be appended to the last one and the looping just keep going.
For appending the first items to the last items, I've tried something like this:
def loop_slicing(lst_, i):
""" Slice iterable repeatedly """
if i[0] > i[1]:
return [n for n in lst_[i[0]:]+lst_[:i[1]]]
else:
return lst_[i[0]:i[1]]
When I call this function, I can do this:
>>> loop_slicing(my_list, (0, 5))
['a', 'b', 'c', 'd', 'e']
>>> loop_slicing(my_list, (25, 4))
['z', 'a', 'b', 'c', 'd']
Where I can just make a generator which can generate 5 sequential numbers in range(0, 26) to loop over my_list and get 5 items each time.
I don't know if this is the best approach. So is there any more efficient way to do the stuff?
Using the itertools module you can cycle and slice a string via an infinite generator:
from itertools import cycle, islice
from string import ascii_lowercase
def gen(x, n):
c = cycle(x)
while True:
yield list(islice(c, n))
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
...
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
Debatably simpler solution using a list comprehension:
def batch_list(ns, batch_size):
return [ns[i:i+batch_size] for i in range(0, len(ns), batch_size)]
>>> batch_list('abcdefghijk', 3)
['abc', 'def', 'ghi', 'jk']
This is a simple construction that I find myself writing often when I want to batch some list of tasks to perform.
EDIT: Just realized the OP asked for the construction to cycle around to the beginning to complete the last batch if needed. This does not do that and will have the last batch truncated.
Thanks for asking,
I took some time to understand the objective of your algorithm but if you want to loop and save all of your sublists I think this should work :
def slicing_items(slc_len = 5, lst, iterate_num = 25):
# slc_len correspond to the number of slices, lst is the list of sequences
n = len(lst)
k = 1
p = k * slc_len
slicing_list = []
while k < iterate_num:
current_slice = []
if p >= n:
for i in range (1, p//n):
current_slice += lst #How many times we passed the length of the list
p = p % n #How many items remaining ?
current_slice += lst[-(slc_len-p):]
current_slice += lst[:p]
else:
current_slice = lst[p-slc_len:p]
k += 1
p += slc_len
slicing_list.append(current_slice)
return slicing_list
Output :
slicing_items(5,my_list,10)
>>> [['a', 'b', 'c', 'd', 'e'],
['f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm', 'n', 'o'],
['p', 'q', 'r', 's', 't'],
['u', 'v', 'w', 'x', 'y'],
['z', 'a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n'],
['o', 'p', 'q', 'r', 's']]
However if you just want the last slice over your iterate_num then your function should fit perfectly (maybe you should use slicing over than rewriting the list in your first boolean statement for rapidity)
Using generator and slicing:
from string import ascii_lowercase
def gen(x, n):
start, stop = 0, n
while True:
if start < stop:
yield list(x[start:stop])
else:
yield ((list(x[start:])) + (list(x[:stop])))
start = stop
stop = (stop + n) % len(x)
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
print(next(G))
print(next(G))
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
print(next(G))
OUTPUT :
['a', 'b', 'c', 'd', 'e']
['f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o']
['p', 'q', 'r', 's', 't']
['u', 'v', 'w', 'x', 'y']
['z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i']
This is a really interesting problem , if you want to "just" solve the problem go for itertools cycle approach , It have already in-built function, But if you want to enjoy the joy of algorithms building, Go with your own solution and try something :
Here i tried with recursion approach, As you said it will keep going so you have to handle recursion by setting your max limit:
import math
import sys
sys.setrecursionlimit(500)
data=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
try:
def recursive_approch(m, x, n, hold=0, value=0, left=0):
print(hold)
max_time = len(m) / n
max_t = int(math.modf(max_time)[1])
left = len(m) % n
if value == max_t:
if len(x) == left:
x = x + m[:-len(x)]
value = 0
left = 0
else:
hold = x[:n]
value = value + 1
return recursive_approch(m, x[n:], n, hold=hold, value=value, left=left)
return recursive_approch(m, x, n, hold=hold, value=value, left=left)
print(recursive_approch(data, data, 6))
except RecursionError:
print('maximum recursion')
You have to pass the number for slice so if you want to slice 6-6 then:
print(recursive_approch(data, data, 6))
output:
['a', 'b', 'c', 'd', 'e', 'f']
['g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r']
['s', 't', 'u', 'v', 'w', 'x']
['s', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
...................
If you want 3-3 then:
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h', 'i']
['j', 'k', 'l']
['m', 'n', 'o']
['p', 'q', 'r']
['s', 't', 'u']
['v', 'w', 'x']
['v', 'w', 'x']
['y', 'z', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
.......
if you pass 12 :
0
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
....

Delete a string from an array in Python

I'm writing some code in Python to read from a file some text, and make a 2-dimensional array from it. But when I make the array, in the last spot of the first 2 array(of three) there is : '\n', and I want delete it.
This is the file(data.txt):
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
And this is the Python code:
data = open("data.txt", mode="r")
arr = data.readlines()
for i in range(len(arr)):
arr[i] = list(arr[i].split(","))
#here I tryed to detele it
for i in range(len(arr)):
if arr[i][len(arr[i])-1] == '\\n':
del arr[len(arr[i])-1]
data.close()
This is the result of the code(but there is anyway '\n'):
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
How I could delete those?
Short solution using str.rstrip() and str.splitlines() functions:
with open('data.txt', 'r') as f:
items = [l.rstrip(',').split(',') for l in f.read().splitlines()]
print(items)
The output:
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']]
You can use rstrip and list comprehension.
with open("data.txt", 'r', encoding="utf-8") as file:
array = [line.rstrip(',\n').split(',') for line in file]
You can filter the list and only keep values that are not "\n":
for i in range(len(arr)):
arr[i] = [b for b in arr[i] if b != "\n"]
You can just strip the \n as you read the lines:
arr = []
with open('data.txt', mode="r") as f:
for line in f.readlines():
arr.append(line.strip(',\n').split(','))
print arr

split long string inside one list to small lists

How do you split this long string of one list to small multi-lists as show on the output ? (I have file has 100 lines)
Num=['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'C', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K', '\n', ',', 'S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A', '\n']
Output should look like this:
['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'K', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K']
['S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A']
First join the elements, strip() leading-trailing whitespace characters, split on a new line \n and comma , and then map them to a list again.
In short:
l1, l2 = map(list, "".join(Num).strip().split('\n,'))
Now, l1, l2 look, respectively:
['S', 'I', 'R', 'T', 'S', 'A', 'V', 'P', 'S', 'P', 'C', 'G', 'K', 'Y', 'Y', 'T', 'L', 'N', 'G', 'S', 'K']
and
['S', 'T', 'P', 'C', 'T', 'T', 'I', 'N', 'K', 'V', 'K', 'A', 'S', 'G', 'M', 'K', 'A', 'I', 'M', 'M', 'A']

Categories