Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Changing values in a dictionary within a dictionary
#1
I am having a problem with over-accessing and changing values in a dictionary within a dictionary using the method 'startswith'.

I have a basic dictionary (the dictionary for which I want this to work is larger, hence the need for functions to do the work for me) for the trial:
 
d = {0:{'lfm':1, 'lfg':1, 'lwg': 1, 'lwm': 1},1:{'lfm': 1, 'lfg': 1, 'lwg': 1, 'lwm': 1}} 
Now I want to change some values in only one of the interior dictionaries, so I define a function:
 
def populate(col, choice, start):
    for key in d[col]:
        if key.startswith(choice, start):#I use startswith because my real keys are longer
            d[col].update({key:0})    # or d[col][key]=0
and use
 
populate(1, 'g', 2)
This should change any value that ends in 'g' within the second inner dictionary, but not the first; yet it changes values that end in 'g' in the first inner dictionary, as can be seen:
d={0: {'lfm':1, 'lfg':0, 'lwg':0, 'lwm':1}, 1: {'lfm':1, 'lfg':0, 'lwg': 0, 'lwm': 1}}
I have done an intermediate print as the last line in the 'populate' function, within the 'if' statement, and it shows that it is picking up the right values but also changing the wrong values.

So why is d[col] not selecting only one dictionary.

Really frustrated.

John
Quote
#2
I'm not observing the same thing as you
Output:
>>> d = {0:{'lfm':1, 'lfg':1, 'lwg': 1, 'lwm': 1},1:{'lfm': 1, 'lfg': 1, 'lwg': 1, 'lwm': 1}} >>> def populate(col, choice, start): ... for key in d[col]: ... if key.startswith(choice, start):#I use startswith because my real keys are longer ... d[col].update({key:0}) # or d[col][key]=0 ... >>> populate(1, 'g', 2) >>> print d {0: {'lfm': 1, 'lfg': 1, 'lwg': 1, 'lwm': 1}, 1: {'lfm': 1, 'lfg': 0, 'lwg': 0, 'lwm': 1}}
I suspect I know what's going on though
Output:
>>> inner = {'lfm':1, 'lfg':1, 'lwg': 1, 'lwm': 1} >>> d = {0: inner, 1: inner} >>> populate(1, 'g', 2) >>> d {0: {'lfm': 1, 'lfg': 0, 'lwg': 0, 'lwm': 1}, 1: {'lfm': 1, 'lfg': 0, 'lwg': 0, 'lwm': 1}}
In this second bit of code, I'm creating a single "inner" dictionary and re-using it. Note that in this case, the dictionary isn't copied, it's just reused. So when it gets mutated, that mutation is visible in two places. When you're constructing your outer dictionary, you probably want to copy the dictionary before inserting it.
Quote
#3
Many thanks for that, and you very perspicaciously diagnosed my error. I was repeating the inner dictionary.

John
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  changing the process command line arguments in python Skaperen 3 126 Aug-19-2019, 02:40 PM
Last Post: wavic
  Pass Dictionary to Method is not possible Frank123456 1 82 Aug-19-2019, 10:18 AM
Last Post: buran
  Dictionary for Excel Data Ranjirock 0 91 Aug-18-2019, 05:26 PM
Last Post: Ranjirock
  Basic one: Aggregating from a dictionary Mustey 1 129 Aug-12-2019, 07:33 PM
Last Post: buran
  Automating to run python script 100 times by changing parameters pmt 0 108 Aug-10-2019, 02:58 PM
Last Post: pmt
  Parsing json - dictionary with in dictionary krish216 6 296 Jul-30-2019, 10:01 PM
Last Post: cvsae
  Create XML from dictionary pygrrrl 7 253 Jul-29-2019, 08:33 PM
Last Post: pygrrrl
  Dictionary help Tridium 3 117 Jul-29-2019, 09:41 AM
Last Post: Tridium
  How do you import a dictionary from a .csv file? SteampunkMaverick12 2 158 Jul-29-2019, 09:20 AM
Last Post: sudheerm
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 139 Jul-25-2019, 11:32 PM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)