This question already has an answer here:
这个问题已经有了答案:
- 'too many values to unpack', iterating over a dict. key=>string, value=>list 7 answers
- “太多的值要解压缩”,迭代一个dict. key=>字符串,value=>列表7答案。
I am getting that exception from this code:
我从这段代码中得到了一个例外:
class Transaction:
def __init__ (self):
self.materials = {}
def add_material (self, m):
self.materials[m.type + m.purity] = m
def serialize (self):
ser_str = 'transaction_start\n'
for k, m in self.materials:
ser_str += m.serialize ()
sert += 'transaction_end\n'
return ser_str
The for
line is the one throwing the exception. The m
s are Material
objects. Anybody have any ideas why?
排队的人是抛出异常的人。ms是物质的对象。有人知道为什么吗?
3 个解决方案
#1
148
self.materials
is a dict
and by default you are iterating over just the keys (which are strings).
自我。材料是一种命令,默认情况下,您只是迭代键(这是字符串)。
Since self.materials
has more than two keys*, they can't be unpacked into the tuple
"k, m
", hence the ValueError
exception is raised.
因为自我。材料有两个以上的键*,它们不能被解压缩到tuple“k, m”中,因此产生了ValueError异常。
In Python 2.x, to iterate over the keys and the values (the tuple
"k, m
"), we use self.materials.iteritems()
.
在Python中2。为了遍历键和值(tuple“k, m”),我们使用self。materials.iteritems()。
However, since you're throwing the key away anyway, you may as well simply iterate over the dictionary's values:
然而,既然你把钥匙扔了,你也可以简单地重复字典的值:
for m in self.materials.itervalues():
In Python 3.x, prefer dict.values()
(which returns a dictionary view object):
Python 3。值()(返回字典视图对象):
for m in self.materials.values():
#2
59
for k, m in self.materials.items():
example:
例子:
miles_dict = {'Monday':1, 'Tuesday':2.3, 'Wednesday':3.5, 'Thursday':0.9}
for k, v in miles_dict.items():
print("%s: %s" % (k, v))
#3
17
Iterating over a dictionary object itself actually gives you an iterator over its keys. Python is trying to unpack keys, which you get from m.type + m.purity
into (m, k)
.
遍历dictionary对象本身实际上会给您一个遍历其键的迭代器。Python正在尝试打开从m获得的密钥。类型+ m。纯度(m,k)。
My crystal ball says m.type
and m.purity
are both strings, so your keys are also strings. Strings are iterable, so they can be unpacked; but iterating over the string gives you an iterator over its characters. So whenever m.type + m.purity
is more than two characters long, you have too many values to unpack. (And whenever it's shorter, you have too few values to unpack.)
我的水晶球说。类型和m。纯是字符串,所以键也是字符串。字符串是可迭代的,因此可以解压;但是遍历字符串会给您一个遍历其字符的迭代器。所以每当m。类型+ m。纯度超过两个字符长,你有太多的值去解包。(只要时间短,你的价值就太少了。)
To fix this, you can iterate explicitly over the items
of the dict, which are the (key, value) pairs that you seem to be expecting. But if you only want the values, then just use the values.
为了解决这个问题,您可以在命令的条目上进行显式迭代,这是您所期望的(键值)对。但如果只需要值,那么就使用值。
(In 2.x, itervalues
, iterkeys
, and iteritems
are typically a better idea; the non-iter
versions create a new list object containing the values/keys/items. For large dictionaries and trivial tasks within the iteration, this can be a lot slower than the iter
versions which just set up an iterator.)
(在2。x、itervalues、iterkeys和iteritems通常是更好的主意;非iter版本创建一个包含值/键/项的新列表对象。对于大型字典和迭代中的琐碎任务,这可能比只设置迭代器的iter版本要慢得多。
#1
148
self.materials
is a dict
and by default you are iterating over just the keys (which are strings).
自我。材料是一种命令,默认情况下,您只是迭代键(这是字符串)。
Since self.materials
has more than two keys*, they can't be unpacked into the tuple
"k, m
", hence the ValueError
exception is raised.
因为自我。材料有两个以上的键*,它们不能被解压缩到tuple“k, m”中,因此产生了ValueError异常。
In Python 2.x, to iterate over the keys and the values (the tuple
"k, m
"), we use self.materials.iteritems()
.
在Python中2。为了遍历键和值(tuple“k, m”),我们使用self。materials.iteritems()。
However, since you're throwing the key away anyway, you may as well simply iterate over the dictionary's values:
然而,既然你把钥匙扔了,你也可以简单地重复字典的值:
for m in self.materials.itervalues():
In Python 3.x, prefer dict.values()
(which returns a dictionary view object):
Python 3。值()(返回字典视图对象):
for m in self.materials.values():
#2
59
for k, m in self.materials.items():
example:
例子:
miles_dict = {'Monday':1, 'Tuesday':2.3, 'Wednesday':3.5, 'Thursday':0.9}
for k, v in miles_dict.items():
print("%s: %s" % (k, v))
#3
17
Iterating over a dictionary object itself actually gives you an iterator over its keys. Python is trying to unpack keys, which you get from m.type + m.purity
into (m, k)
.
遍历dictionary对象本身实际上会给您一个遍历其键的迭代器。Python正在尝试打开从m获得的密钥。类型+ m。纯度(m,k)。
My crystal ball says m.type
and m.purity
are both strings, so your keys are also strings. Strings are iterable, so they can be unpacked; but iterating over the string gives you an iterator over its characters. So whenever m.type + m.purity
is more than two characters long, you have too many values to unpack. (And whenever it's shorter, you have too few values to unpack.)
我的水晶球说。类型和m。纯是字符串,所以键也是字符串。字符串是可迭代的,因此可以解压;但是遍历字符串会给您一个遍历其字符的迭代器。所以每当m。类型+ m。纯度超过两个字符长,你有太多的值去解包。(只要时间短,你的价值就太少了。)
To fix this, you can iterate explicitly over the items
of the dict, which are the (key, value) pairs that you seem to be expecting. But if you only want the values, then just use the values.
为了解决这个问题,您可以在命令的条目上进行显式迭代,这是您所期望的(键值)对。但如果只需要值,那么就使用值。
(In 2.x, itervalues
, iterkeys
, and iteritems
are typically a better idea; the non-iter
versions create a new list object containing the values/keys/items. For large dictionaries and trivial tasks within the iteration, this can be a lot slower than the iter
versions which just set up an iterator.)
(在2。x、itervalues、iterkeys和iteritems通常是更好的主意;非iter版本创建一个包含值/键/项的新列表对象。对于大型字典和迭代中的琐碎任务,这可能比只设置迭代器的iter版本要慢得多。