我有一个包含列表的列表,我想在 w/o 列中打印要导入的任何其他模块(即 pprint
)。该任务仅供我了解列表的迭代。这是我的 list :
tableData = [['a', 'b', 'c', 'd'],
['1', '2', '3', '4'],
['one', 'two', 'three', 'four']]
我希望它看起来像这样:
a 1 one
b 2 two
c 3 three
d 4 four
我设法对它的第一行进行了一些硬编码,但我不知道如何为迭代建模。见:
def printTable(table):
print(table[0][0].rjust(4," ") + table[1][0].rjust(4," ") + table[2][0].rjust(4," "))
最佳答案
您可以使用 zip()
像这样:
>>> for v in zip(*tableData):
print (*v)
a 1 one
b 2 two
c 3 three
d 4 four
你显然可以改进格式(就像@Holt 做得很好)但这是基本方法:)
关于python - 在列中的列表中打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580061/