【发布时间】:2017-12-05 17:49:29
【问题描述】:
我以为我为自己制定了一个简单的项目,但我想不是。我认为我长期使用 Ordered dict 函数,因为我不断得到:
ValueError: too many values to unpack (expected 2)
代码:
import random
import _collections
shop = {
'bread': 2,
'chips': 4,
'tacos': 5,
'tuna': 4,
'bacon': 8,
}
print(shop)
'''
items = list(shop.keys())
random.shuffle(items)
_collections.OrderedDict(items)
'''
n = random.randrange(0, len(shop.keys()))
m = random.randrange(n, len(shop.keys()))
if m <= n:
m += 1
print(n, " ", m)
for key in shop.keys():
value = shop[key] * random.uniform(0.7,2.3)
print(key, "=", int(value))
if n < m:
n += 1
else:
break
我希望这段代码混合字典,然后将值乘以 0.7 - 2.3。然后在范围内循环 0-5 次,以便从字典中给我一些随机键。
我已将 ''' ''' 放置在我难以处理的代码上并给出错误。
【问题讨论】:
-
您根本没有使用 OrderedDict。您只有一个字符串文字,看起来像使用 OrderedDict 的代码(并且做错了,尝试在键列表上调用 OrderedDict,甚至没有尝试使用构造的 OrderedDict)。
标签: python python-3.x dictionary ordereddictionary