“太多的值无法解压”,迭代字典。键=>字符串,值=>列表

2024-03-24

我正在得到too many values to unpack错误。知道我该如何解决这个问题吗?

first_names = ['foo', 'bar']
last_names = ['gravy', 'snowman']

fields = {
    'first_names': first_names,
    'last_name': last_names,
}        

for field, possible_values in fields:  # error happens on this line

Python 3

Use items() http://docs.python.org/library/stdtypes.html#dict.items.

for field, possible_values in fields.items():
    print(field, possible_values)

Python 2

Use iteritems() http://docs.python.org/library/stdtypes.html#dict.iteritems.

for field, possible_values in fields.iteritems():
    print field, possible_values

See 这个答案 https://stackoverflow.com/a/3294899/1489538有关迭代字典的更多信息,例如使用items(),跨 Python 版本。

以供参考,iteritems()在 Python 3 中被删除 https://docs.python.org/3/whatsnew/3.0.html#views-and-iterators-instead-of-lists.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“太多的值无法解压”,迭代字典。键=>字符串,值=>列表 的相关文章

随机推荐