python - '太多的值无法解包',迭代一个字典。键=>字符串,值=>列表

我收到 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

使用 items() .

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

Python 2

使用 iteritems() .

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

见 this answer有关在 Python 版本中迭代字典(例如使用 items())的更多信息。

供引用,iteritems() was removed in Python 3 .

关于python - '太多的值无法解包',迭代一个字典。键=>字符串,值=>列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5466618/

相关文章:

python - 如何在 Python 中获取当前模块中所有类的列表?

python - 测试 Python 中是否存在可执行文件?

python - 为什么 'x' 中的 ('x' 比 'x' == 'x' 快?

python - 默认字典的默认字典?

python - 从 Django QuerySet 获取 SQL

python - 得到一系列列表的笛卡尔积?

python - 将标准输出重定向到 Python 中的文件?

python - 在 Python 3 中从 Web 下载文件

python - 如何计算 ndarray 中某个项目的出现次数?

python - 如何将 time.struct_time 对象转换为 datetime 对象?