I am trying to understand how to print the following table of numbers to get the desired output:
tableData = [
['1','2','3','4'],
['11','12','13','14'],
['21','22','23','24']
]
The output:
1 11 21
2 12 22
3 13 23
4 14 24
After digging around a bit, I was able to write this following code that gives me the correct answer:
for row in range(len(tableData[0])):
for item in range(len(tableData)):
print(tableData[item][row], end = " ")
print('')
However, I do not understand one thing. In the first line of code, why is the [0] needed? When I remove it the code does not print out the last line 4, 14, 24, and I do not understand why. If someone can explain the logic of adding [0] here I would appreciate it a lot.
np.array(tableData).transpose()tableData[0]is just to get the length of each list, which in turn is the number of rows in your output. Usually 0 is used because is the first element of the table and we know the list must contain at least one element.for column in range(len(tableData[0]))might be clearer. Of course, you could be thinking of the number ofoutputrows...but I don't think most people would think that way.