“太多的价值无法解包”,遍历一个字典。键=>字符串,值=>列表

问题:“太多的价值无法解包”,遍历一个字典。键=>字符串,值=>列表

我得到了'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              

I am getting the 'too many values to unpack' error. Any idea how I can fix this?

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              

回答 0

Python 2

您需要使用iteritems

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

请参阅此答案,以获取有关遍历字典的更多信息,例如items()在python版本之间使用using 。

Python 3

由于Python 3的 iteritems()不再支持。使用items()代替。

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

Python 2

You need to use something like iteritems.

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

See this answer for more information on iterating through dictionaries, such as using items(), across python versions.

Python 3

Since Python 3 iteritems() is no longer supported. Use items() instead.

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

回答 1

对于Python 3.x iteritems,已删除。改用物品

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

For Python 3.x iteritems has been removed. Use items instead.

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

回答 2

您要使用iteritems。这将在字典上返回一个迭代器,从而为您提供一个元组(键,值)

>>> for field, values in fields.iteritems():
...     print field, values
... 
first_names ['foo', 'bar']
last_name ['gravy', 'snowman']

您的问题是您正在遍历字段,该字段返回字典的键。

>>> for field in fields:
...     print field
... 
first_names
last_name

You want to use iteritems. This returns an iterator over the dictionary, which gives you a tuple(key, value)

>>> for field, values in fields.iteritems():
...     print field, values
... 
first_names ['foo', 'bar']
last_name ['gravy', 'snowman']

Your problem was that you were looping over fields, which returns the keys of the dictionary.

>>> for field in fields:
...     print field
... 
first_names
last_name

回答 3

对于列表,使用 enumerate

for field, possible_values in enumerate(fields):
    print(field, possible_values)

iteritems 不适用于列表对象

For lists, use enumerate

for field, possible_values in enumerate(fields):
    print(field, possible_values)

iteritems will not work for list objects


回答 4

在Python3 iteritems()中不再受支持

解决方案1

采用 .items

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

解决方案2

您可以使用enumerate(),以及

for field, possible_values in enumerate(fields):
    print(field, possible_values)

In Python3 iteritems() is no longer supported

SOLUTION1:

Use .items

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

SOLUTION2:

You can use enumerate() as well

for field, possible_values in enumerate(fields):
    print(field, possible_values)

回答 5

不能直接在字典中进行迭代。这样就可以通过转换为元组了

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

fields = {
    'first_names': first_names,
    'last_name': last_names,
         } 
tup_field=tuple(fields.items())
for names in fields.items():
     field,possible_values = names
     tup_possible_values=tuple(possible_values)
     for pvalue in tup_possible_values:
           print (field + "is" + pvalue)

Can’t be iterating directly in dictionary. So you can through converting into tuple.

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

fields = {
    'first_names': first_names,
    'last_name': last_names,
         } 
tup_field=tuple(fields.items())
for names in fields.items():
     field,possible_values = names
     tup_possible_values=tuple(possible_values)
     for pvalue in tup_possible_values:
           print (field + "is" + pvalue)

回答 6

fields.iteritems()在代码中丢失了。

您也可以采用其他方式,即使用字典中的键获取值。

for key in fields:
    value = fields[key]

you are missing fields.iteritems() in your code.

You could also do it other way, where you get values using keys in the dictionary.

for key in fields:
    value = fields[key]

回答 7

data = (['President','George','Bush','is','.'],['O','B-PERSON','I-PERSON','O','O'])
corpus = []
for(doc,tags) in data:
    doc_tag = []
    for word,tag in zip(doc,tags):
        doc_tag.append((word,tag))
        corpus.append(doc_tag)
        print(corpus)
data = (['President','George','Bush','is','.'],['O','B-PERSON','I-PERSON','O','O'])
corpus = []
for(doc,tags) in data:
    doc_tag = []
    for word,tag in zip(doc,tags):
        doc_tag.append((word,tag))
        corpus.append(doc_tag)
        print(corpus)