这里的,我认为是可能接近你在寻找什么解决办法:
import random
cave_names = ['A','B','C','D','E','F','G']
item_names = ['coins', 'power', 'knives', 'beer', 'pistol']
# Create the dictionary of caves, all of which have no items to start
caves = {cave : {item : 0 for item in item_names} for cave in cave_names}
# Randomly distribute the dwarf's items into the caves
dwarf_items = {'coins' : 30, 'power' : 11, 'knives' : 20, 'beer' : 10, 'pistol' : 2}
for key, value in dwarf_items.iteritems():
for i in range(value):
# Give away all of the items
cave = random.choice(cave_names)
caves[cave][key] += 1
# Take the item away from the dwarf
dwarf_items[key] -= 1
print(caves)
下面是在到底是什么洞穴看起来,所有的侏儒的项目后就一直一个例子随机分配到洞穴:
{'A': {'beer': 2, 'coins': 4, 'knives': 1, 'pistol': 1, 'power': 1},
'B': {'beer': 0, 'coins': 3, 'knives': 7, 'pistol': 0, 'power': 0},
'C': {'beer': 1, 'coins': 2, 'knives': 1, 'pistol': 0, 'power': 3},
'D': {'beer': 3, 'coins': 8, 'knives': 3, 'pistol': 0, 'power': 2},
'E': {'beer': 2, 'coins': 4, 'knives': 2, 'pistol': 1, 'power': 5},
'F': {'beer': 2, 'coins': 7, 'knives': 5, 'pistol': 0, 'power': 0},
'G': {'beer': 0, 'coins': 2, 'knives': 1, 'pistol': 0, 'power': 0}}
洞穴不是一本字典。这里有很多语法错误。 –