How can I add the elements of a character list into an nxm array?
I have this code in python.
key = raw_input('Key: ') #Pedimos la clave
text = raw_input('Text: ') #Pedimos una cadena de texto
text_c = list(text) #Pasamos la cadena a una lista de caracteres
while ' ' in text_c: #quitamos los espacios
text_c.remove(' ')
#caculo de las columnas y renglones
columns = len(key)
rows = int(math.ceil(float(len(text_c)) / float(len(key))) )
#Definicion de una matriz
matrix = []
for i in range(rows):
matrix.append([])
for j in range(columns):
matrix[i].append(None)
What I want to do is fill the array with the characters from the list.
For example the user types: key:HELLO , text: this is text
The program generates a matrix of 3 rows by 4 columns and I want to fill that matrix with the text characters, in this way:
[e,s,t,o]
[e,s,u,n]
[t,e,x,t]
[o,none,none,none]
Thank you for your answers
You practically had it done. Following your example, this is what was missing:
Define a variable to count the characters
Increment for each iteration
That, as the 2 loops progress, it increases by 1
Add the character of that position as a new column in the row
i
... except when there are no more characters left
Code
Result:
[['e', 's', 't', 'o'], ['e', 's', 'u', 'n'], ['t', 'e', 'x', 't'], ['o', None, None, None]]
Demo en ideone.com
It should be noted that there are more compact or pythonic ways to do this, but it seemed to me the best option to explain the subject.
I was able to solve it in the following way. In case someone finds it useful, this is the code: