Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 组合词典_Python_List_Random_Dictionary - Fatal编程技术网

Python 组合词典

Python 组合词典,python,list,random,dictionary,Python,List,Random,Dictionary,我是python初学者。如果我想合并以下两种词典,该怎么办: dwarf items={'coins':30,'power':11,'Knives':20,'beer':10,'pistol':2} caves=[('A','B','C','D','E','F','G')] 我想让“侏儒”在任何一个洞穴里随机扔东西 我尝试了zip函数,但没有按预期的方式工作。 输出应如下所示: cache={'A':0,'B':['coins':30],'C':['Knives':20},'D':0,'E':

我是python初学者。如果我想合并以下两种词典,该怎么办:

dwarf items={'coins':30,'power':11,'Knives':20,'beer':10,'pistol':2}
caves=[('A','B','C','D','E','F','G')]
我想让“侏儒”在任何一个洞穴里随机扔东西

我尝试了zip函数,但没有按预期的方式工作。 输出应如下所示:

cache={'A':0,'B':['coins':30],'C':['Knives':20},'D':0,'E':0,'F':0,'G':0}

我认为您可能正在寻找以下内容:

dwarf_items = {'coins': 30, 'power': 11, 'Knives': 20, 'beer': 10, 'pistol': 2}
caves = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
drop_chance = 0.4  # change this to make it more or less likely an item will drop
cache = {}
for cave in caves:
    if dwarf_items and random.random() < drop_chance:
        item = random.choice(dwarf_items.keys())
        cache[cave] = {item: dwarf_items.pop(item)}
    else:
        cache[cave] = {}



这里有一个解决方案,我认为可能与您所寻找的非常接近:

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}}

洞穴不是字典。您在这里有很多语法错误。是否可以只获取一个随机副本并将项目分别放入该列表中更好?这些代码对我很有用,您使用的是什么版本?您可能需要在代码顶部添加
import random
。这是Python2.7,但在其他版本中也应该可以使用。这一个不起作用,错误消息是“dict”没有“iteritems”属性@user3285116您必须使用Python3。iteritems()已重命名为simply items()
>>> cache
{'A': {}, 'C': {}, 'B': {}, 'E': {'beer': 10}, 'D': {}, 'G': {}, 'F': {}}
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}}