#Given a dictionary
>>> test ={'line4': (4, 2), 'line3': (3, 2), 'line2': (2, 2), 'line1': (1, 2), 'line10': (10, 2)}
#And if you want a list of tuples, what you need actually is the values of the dictionary
>>> test.values()
[(4, 2), (3, 2), (2, 2), (1, 2), (10, 2)]
#Instead if you want a flat list of values, you can flatten using chain/chain.from_iterable
>>> list(chain(*test.values()))
[4, 2, 3, 2, 2, 2, 1, 2, 10, 2]
#And to print the list
>>> for v in chain.from_iterable(test.values()):
print v
4
2
3
2
2
2
1
2
10
2
这里有一些可能性。你的问题很模糊,你的代码甚至还不能正常工作,所以很难理解这个问题
正在分析代码
^{pr2}$for key in test
按键迭代字典for key in test.values()
按值迭代字典试试这个:
如果您需要列表:
^{pr2}$如果要从dict than打印每条记录:
相关问题 更多 >
编程相关推荐