在python中打印列表输出的最佳方法 [英] Best way to print list output in python
本文介绍了在python中打印列表输出的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的list
和list of list
>>> list2 = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"]]
>>> list1 = ["a","b","c"]
我压缩了上面两个列表,以便可以按索引匹配它们的值索引.
I zipped the above two list so that i can match their value index by index.
>>> mylist = zip(list1,list2)
>>> mylist
[('a', ['1', '2', '3', '4']), ('b', ['5', '6', '7', '8']), ('c', ['9', '10', '11', '12'])]
现在我尝试使用
>>> for item in mylist:
... print item[0]
... print "---".join(item[1])
...
结果是我的desired output
.
a
1---2---3---4
b
5---6---7---8
c
9---10---11---12
现在,我的问题是还有一种cleaner and better
方式可以达到我想要的输出,或者这是best(short and more readable)
可能的方式.
Now, my question is there a more cleaner and better
way to achieve my desired output or this is the best(short and more readable)
possible way.
推荐答案
好吧,您可以避免一些临时变量并使用更好的循环:
Well, you could avoid some temporary variables and use a nicer loop:
for label, vals in zip(list1, list2):
print label
print '---'.join(vals)
不过,我认为您不会从根本上获得更好的收益.
I don't think you're going to get anything fundamentally "better," though.
这篇关于在python中打印列表输出的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文