【发布时间】:2012-05-29 07:11:35
【问题描述】:
我有一个像这样的list 和一个list of list
>>> list2 = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"]]
>>> list1 = ["a","b","c"]
我压缩了以上两个列表,以便我可以按索引匹配它们的值索引。
>>> mylist = zip(list1,list2)
>>> mylist
[('a', ['1', '2', '3', '4']), ('b', ['5', '6', '7', '8']), ('c', ['9', '10', '11', '12'])]
现在我尝试使用上面的mylist 打印输出
>>> 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) 可能的方式。
【问题讨论】:
-
不是真的,你可以写
for name,lst in mylist: print name而不是索引,但我认为仅此而已 -
另一条评论——如果这两个列表真的要并排维护,您可能需要考虑将它们存储为字典...
-
@mgilson:谢谢。我一定会探索这个选项。