Python Forum
Building a dictionary .update entry from variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a dictionary .update entry from variables
#1
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
Reply


Messages In This Thread
Building a dictionary .update entry from variables - by Curbie - Sep-02-2024, 03:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 5,001 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  update values in list based on dictionary bunti 3 4,341 Jun-10-2019, 07:26 AM
Last Post: perfringo
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 42,729 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  variables help / update config file mapvis 3 5,123 Nov-27-2018, 02:24 PM
Last Post: mapvis
  Dictionary using variables as a keys andresgt 4 6,956 Oct-10-2017, 07:04 AM
Last Post: buran
  Building a patterned grid from a dictionary... xepicxmonkeyx 3 25,294 Oct-01-2016, 06:39 PM
Last Post: xepicxmonkeyx

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.