将Python字典压缩并一起列出 [英] Zipping a Python Dictionary and List together
本文介绍了将Python字典压缩并一起列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将python字典和列表一起压缩?
Is it possible to Zip a python Dictionary and List together?
例如:
dict = {'A':1, 'B':2, 'C':3}
num_list = [1, 2, 3]
zipped = zip(dict, num_list)
然后我想做这样的事情:
Then I'd like to do something like this:
for key, value, num_list_entry in zipped:
print key
print value
print num_list_entry
我还没有找到解决方案,所以我想知道如何去做?
I haven't found a solution for this, so I'm wondering how do I go about doing this?
推荐答案
您可以使用iteritems()
.
dictionary = {'A':1, 'B':2, 'C':3}
num_list = [1, 2, 3]
zipped = zip(dictionary.iteritems(), num_list)
for (key, value), num_list_entry in zipped:
print key
print value
print num_list_entry
这篇关于将Python字典压缩并一起列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文