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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
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
  Remove isolated vertices from dictionary and adjacency matrix Weird 1 80 Jan-18-2020, 04:33 PM
Last Post: Weird
  dictionary merge Skaperen 5 183 Jan-17-2020, 01:28 AM
Last Post: Skaperen
  Python dictionary help kamaleon 3 108 Dec-30-2019, 06:22 PM
Last Post: kamaleon
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 284 Dec-27-2019, 05:31 PM
Last Post: etjkai
  Need to create a dictionary from a *.csv 74razor 5 189 Dec-18-2019, 08:57 PM
Last Post: ichabod801
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 121 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  accessing Json dictionary lshankar 1 116 Dec-16-2019, 06:18 AM
Last Post: ndc85430
  Dictionary VS Class JKBarnard 2 203 Dec-14-2019, 05:37 PM
Last Post: JKBarnard
  Not equal a dictionary key value bazcurtis 2 142 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  why is this dictionary giving a Keyerror? philipbergwerf 1 96 Dec-11-2019, 08:03 PM
Last Post: DreamingInsanity

Forum Jump:


Users browsing this thread: 1 Guest(s)