Adding new key to dictionary overwrites all previo

2019-03-02 15:09发布

问题:

I'm trying to use a for loop to generate random values for item prices, by changing the value of the item prices in a pre-defined dictionary.

The new values of this pre-defined dictionary are then added to the end of another pre-defined dictionary so a history of prices can be stored.

here is my code:

tradable_good_prices= {'iron' : 0, 'gold' : 0, 'lead' : 0, 'ruby' : 0 'emerald' : 0, 'steel' : 0, 'diamond' : 0} 
item_list = tradable_good_prices.keys() 
item_price_history = {}

def Random_economy(generations):

    for generation_number in range(0, generations): 

        for list_number in range(0, len(item_list)): 

            tradable_good_prices[item_list[list_number]] = np.random.random_integers(100,1000) 

        print(tradable_good_prices)

        item_price_history[generation_number] = tradable_good_prices 

        print(item_price_history)

Random_economy(2)

the code takes in generations as an argument for the number of for loop iterations. And using a value of 2 for generations this output is produced on the console:

{'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}

{0: {'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}}

{'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}

{0: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}, 1: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}}

As can be seen the previous values are being overwritten, I'm guessing theres quite a simple explanation for this like "the dictionary storing the different generation values is referencing the first one for its values" but I cannot find help on this matter anywhere.

So could someone please explain to me what I'm doing wrong.

回答1:

The keys in a dictionary are unique. If a key exists in a dictionary, d[key] = other_value just changes the value for that key, it does NOT create another item.

>>> d = {'a':1, 'b':'foo'}
>>> d['b'] = 'six'
>>> d
{'b': 'six', 'a': 1}
>>> d.update([('a','bar')])
>>> d
{'b': 'six', 'a': 'bar'}
>>>

If you have data that you want to place in a dictionary and the data contains keys with multiple values, you could put the values into a list for each key. collections.defaultdict makes this easy.

>>> a
[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('a', 100), ('c', 99), ('d', 98), ('f', 97)]
>>> import collections
>>> d = collections.defaultdict(list)
>>> for key, value in a:
    d[key].append(value)

>>> d
defaultdict(<class 'list'>, {'b': [1], 'a': [0, 100], 'e': [4], 'f': [5, 97], 'd': [3, 98], 'c': [2, 99]})
>>> 

For your problem, start with the initial values in a list then add to them.

import random

d = {'a':[0], 'b':[0], 'c':[0]}
for _ in xrange(4):
    for key in d:
        d[key].append(random.randint(1, 100))

for item in d.items():
    print item

>>>
('a', [0, 92, 45, 52, 32])
('c', [0, 51, 85, 72, 4])
('b', [0, 47, 7, 74, 59])
>>>

How to iterate over a dictionary.