[英]A better way to print nested arrays
Let's say I have this nested array: 假设我有这个嵌套数组:
foo = [['bar', 'bar'], ['bar', 'bar']]
I've usually printed it out like this, using nested for
loops: 我通常使用嵌套的
for
循环这样打印出来:
for array in foo:
for bar in array:
print bar
Since I felt this question was too insufficient for Codereview, I'm asking here, is there any better way to do this than my current method? 由于我觉得这个问题对于Codereview来说还不够,我在这里问,有没有比我目前的方法更好的方法了?
您可以执行以下操作以单独打印出元素:
print('\n'.join(itertools.chain(*foo)))
Have a look at the pprint
module. 看一下
pprint
模块。 You can do something like this: 您可以执行以下操作:
from pprint import pprint
pprint(foo, width=3) // width = 80 but your sequence is shorter than a line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.