Python Forum
Build a multi-variable dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Build a multi-variable dictionary
#1
I want to build a multi-variable dictionary.  Below is an abbreviated version:
Table = {BA: 5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4}
Ultimately, the Table will have about 40 keys and each one will have 35 variables.

I am using Pycharm, and when I keyed in the above (just getting started), I received an "unresolved reference" on the "BA", and I received an "expected end of statement" at the end of the } on the right.

Why the unresolved reference?  Can a dictionary have 35 keys?  Is the syntax of coding a multi-variable dictionary than having one variable?  

As you may be able to tell, I am very new to Python.

thanks for looking
Reply
#2
it looks like you want to create a dict where key is string and value is tuple/list
e.g.

>>> Table = {'BA': (5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4), 
             'DC': (2000, 1000, 500, 250, 125, 0.1, 0.2, 0.3, 0.15, 0.05)}
>>> Table['BA']
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table['DC'][2]
500
Reply
#3
Under your first example Table = {'BA': (5000, 6000, .......) -- I would be able to lookup (using .get, I think) the 'BA', and then retrieve all 35 variables associated with that key. Correct???


thanks for responding
Reply
#4
yes, you can access  the value for given key using get. In my post I had given an example how you can access value for given key and also how can access element in that value. Here is the example again
>>> Table = {'BA': (5000, 6000, 7000, 8000, 10000, .5, .6, .8, .5, .4), 
             'DC': (2000, 1000, 500, 250, 125, 0.1, 0.2, 0.3, 0.15, 0.05)}
>>> Table.get('BA')
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table['BA']
(5000, 6000, 7000, 8000, 10000, 0.5, 0.6, 0.8, 0.5, 0.4)
>>> Table.get('BA')[4]
10000
>>> Table['BA'][4]
10000
or iterate over the elements in the value associated with the key:
>>> for value in Table['BA']:
    print value
Output:
5000 6000 7000 8000 10000 0.5 0.6 0.8 0.5 0.4
Reply
#5
thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  variable as dictionary name? diazepam 4 11,164 Apr-27-2020, 08:11 AM
Last Post: deanhystad
  Reference new dictionary keys with a variable slouw 4 2,883 May-07-2019, 03:30 AM
Last Post: slouw
  Dictionary multi dimension support sripylearn 3 3,003 Aug-14-2018, 03:50 PM
Last Post: buran
  Dictionary named after variable value Alfred 1 3,053 Sep-02-2017, 01:24 PM
Last Post: sparkz_alot
  calling an object variable in a dictionary sunhear 3 4,285 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020