[英]How to display a 2D array in a grid form?
I am trying to learn 2D arrays and how they work, I tried displaying data provided in a grid/table like form, however, it is displaying it by rows and not by column.我正在尝试学习 2D arrays 及其工作原理,我尝试显示以网格/表格形式提供的数据,但是,它是按行而不是按列显示的。
The following list of input will show what I mean:以下输入列表将显示我的意思:
DESIRED INPUT:期望的输入:
exList= ["Table1", "Table2", "Table3"], ["Test1", "Test2", "Test3"], ["Example1", "Example2", "Example 3"]```
DESIRED OUTPUT:所需 OUTPUT:
Table1 Test1 Example1
Table2 Test2 Example2
Table3 Test3 Example3
My code right now:我现在的代码:
exList= ["Table1", "Table2", "Table3"], ["Test1", "Test2", "Test3"], ["Example1", "Example2", "Example 3"]
for row in range(len(exList)):
for col in range(len(exList[row])):
print(exList[row][col], end=" ")
print()
Use zip
to iterate over the lists in parallel使用
zip
并行遍历列表
ex_list = ["Table1", "Table2", "Table3"], ["Test1", "Test2", "Test3"], ["Example1", "Example2", "Example 3"]
for table, test, example in zip(*ex_list):
print(table, test, example)
Output: Output:
Table1 Test1 Example1
Table2 Test2 Example2
Table3 Test3 Example 3
Here you go, just change row and col in exList index在这里你 go,只需更改 exList 索引中的行和列
exList= ["Table1", "Table2", "Table3"], ["Test1", "Test2", "Test3"], ["Example1", "Example2", "Example 3"]
for row in range(len(exList)):
for col in range(len(exList[row])):
print(exList[col][row], end=" ")
print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.