擅长:python、mysql、java
<p>我想你可能在找以下的东西:</p>
<pre><code>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] = {}
</code></pre>
<p>以下是我在几次运行中得到的一些输出示例:</p>
^{pr2}$
<hr/>
<pre><code>>>> cache
{'A': {}, 'C': {'power': 11}, 'B': {'pistol': 2}, 'E': {'Knives': 20}, 'D': {'beer': 10}, 'G': {}, 'F': {'coins': 30}}
</code></pre>
<hr/>
^{4}$