![]() |
| Building a dictionary .update entry from variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Building a dictionary .update entry from variables (/thread-43161.html) |
Building a dictionary .update entry from variables - Curbie - Sep-02-2024 After several weeks of doing 3 passes on 40+ tutorials sticking to the path, the way the author teaches them, hoping that unresolved questions will be answered later in these tutorial(s), this pass I’m trying to answer questions raised by the tutorials themselves Running test code, trying to figure out the answers to some of these questions, like with dictionaries, having a string key name, with an integer value, curmfd.update({"[1,2]": 76}) seems to work to add a string key, with an integer value, but I'm having trouble building a variable for curmfd.update to do the same thing. I'm using it as a lookup structure to store associated integer values to be used later, these will be built in a loop. Can anyone point me to a working example? curmfd = {} # start blank dictionary p = 2 # project number pn = 2 # programmer number lnk = 200 # ppn block link # ppn = f"[{p},{pn}]: {lnk}" # input PPN number ppn = [1,4] # ppn = '{[' + str(p) + ',' + str(pn) + ']: ' + lnk + '}' curmfd.update({"[1,2]": 76}) # add key '[1,2]', value 76 curmfd.update({"[1,4]": 78}) # add key '[1,4]', value 78 curmfd.update({"[1,6]": 164}) # add key '[1,6]', value 164 # curmfd.update(ppn) # add key '[2,2]', value 200 # x = curmfd["[2,2]"] # get value for key '1,4' curmfd.update({"[7,0]": 220}) # add key '[7,0]', value 220 # print(str(x)) # print value for key '2,2' for t in curmfd.keys(): print(t + " ", end='') # print keys print(curmfd[t]) # print values RE: Building a dictionary .update entry from variables - Gribouillis - Sep-02-2024 Use this perhaps curmfd[f"[{p},{pn}]"] = lnkAlso why use a string to store a pair of integers? You could have a dictionary which keys are tuplescurfmd[(p, pn)] = lnk RE: Building a dictionary .update entry from variables - Curbie - Sep-02-2024 Gribouillis thanks, I didn’t want to bore people with arcane and obscure OS file structures, which is where all my posts eventually lead back to, this is the disk-images part of my “DEC pack, unpack and disk-images” post. I have beginning python code reading an old OS digital SD device using lists, but a buddy who also plays with these old machines and python used it thought I should look into replacing “lists” with “dictionaries”, thus these questions. I’ll go through your examples to see if one of those will cure my issue. curbie RE: Building a dictionary .update entry from variables - deanhystad - Sep-02-2024 You don't normally use "update" add 1 item to a dictionary. Save that for adding multiple items. curmfd.update({"[1,2]": 76, "[1,4]": 7, "[1,6]": 164})For a single item use assignment.curmfd["[1,2]"] = 76 curmfd["[1,4]"] = 78 curmfd["[1,6]"] = 164It looks better if you replace "[1,2]" with a tuple curmfd[(1, 2)] = 76Very seldom do you need to use the keys() method for a dictionary. Iterating through a dictionary iterates through the keys. for key in curmfd: print(key, curmfd[key])Using .items() for key, value in curmfd.items(): print(key, value) RE: Building a dictionary .update entry from variables - DeaD_EyE - Sep-02-2024 Use a tuple for keys instead of a str. A list is mutable and cannot used as a key for dicts. curmfd = {} p = 2 pn = 2 lnk = 200 # keys of a dict must be hashable # only immutable objects are hashable # # following Types are hashable: # int, float, complex, Decimal, Fraction # bytes, str, tuple, frozenset # functions, classes, instances of classes where __hash__ != None, curmfd.update({(1, 2): 76}) curmfd.update({(1, 4): 78}) curmfd.update({(1, 6): 164}) curmfd.update({(7, 0): 220}) # don't forget to call the method items() # items() returns an iterator, which yield items (key, value) for key, value in curmfd.items(): print(f"{key} => {value}") # sorting def by_value(item): return item[1] sorted_by_value = dict(sorted(curmfd.items(), key=by_value)) sorted_by_key = dict(sorted(curmfd.items())) RE: Building a dictionary .update entry from variables - Curbie - Sep-03-2024 thanks DeaD_EyE, More newbee turds, will go back and clean it up. |