Sorry for the noob-level question, but I'm trying to take a list of 10 names and put them into a 3x4 matrix, but I'm unsure how to do this when the input of the values was made by a loop.
So instead of going student0 = raw_input("Enter student 1's name: ") ten times I have
students = []
for num in range(1, 11):
students.append({
"name": raw_input("Enter Student %i's name: " % num),
"absences": raw_input("Enter Student %i's absences: " % num)
})
but with the second (and more preferred) form, I don't know how to make the matrix. Before I could just type something like:
print("\n\n Student Seating: \n")
matrix = [[student0 + '\t\t', student1 + '\t\t', student2 + '\t\t'], [student3 + '\t\t', student4 + '\t\t', student5 + '\t\t'], [student6 + '\t\t', 'Empty' + '\t\t', student7 + '\t\t'], ['Empty' + '\t\t', student8 + '\t\t', student9 + '\t\t']]
for row in matrix:
print ' '.join(row)
But now (obviously) that doesn't work.. How would I fix this so I can take my list and put it into matrix (3x4) format? Sorry again for the stupid question, and thanks very much in advance for any help!
You could do this to partition groups of students into sub lists.
>>> students = [1,2,3,4,5,6,7,8,9,10] # assume numbers are students
>>> matrix = [ students[i:i+4] for i in range(0,len(students),4) ]
>>> for l in matrix:
... print l
...
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10]
This has the advantage that no matter what the size of your students list, matrix will always have a max width 4.
If you are comfortable with NumPy,
import numpy as np
a = np.array([0,1,2,3,4,5,6,7,8,9,-1,-1])
print a.reshape((3,4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With