Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary my best option?
#1
I am still a beginner and would like to know the best way to approach my task -- whether to structure my data table as a list or dictionary or something else?

The best way to describe my data table is to picture an Excel table with 50 rows and 47 columns.

Column 1 consists of 50 unique two-letter codes (this would by my "key" in a dictionary).  Columns 2 through 48 have variables/numbers (one per column) associated/linked with the unique two-letter code in column 1.

As a beginner, I have used a dictionary to ".get" one variable associated with a unique key.

However, what if I need to .get 47 variables based on the two-letter unique key?  Is that possible with a dictionary?  Is it easy to grab/identify each variable with some kind of loop or something? 

I know this may not be enough information, but it is the best way for me to describe my task
thanks for looking
Reply
#2
Quote:As a beginner, I have used a dictionary to ".get" one variable associated with a unique key.

However, what if I need to .get 47 variables based on the two-letter unique key?  Is that possible with a dictionary?  Is it easy to grab/identify each variable with some kind of loop or something? 
yes a for loop. 

You can also just return a list of keys via MY_DICTIONARY.keys() and a list of values with MY_DICTIONARY.values()

If you need to specify the key and not get all you can just loop them and insert an if condition to get the key/value pairs you want

for k,v in MY_DICTIONARY.items():
    #insert if condition here to check key for more filtering
    #k is the key
    #v is the value of that key
Post your code for more explicit help
Recommended Tutorials:
Reply
#3
thanks for the reply -- I need to learn a little more about dictionaries. Happy New Year
Reply
#4
Hello!
You can feed a dictionary key with every type of data, objects. You can have nested structures of data. 
Or this:

In [1]: def foo():
   ...:     return 'foo'
   ...: 

In [2]: def bar():
   ...:     return 'bar'
   ...: 

In [3]: d = {'bob': [foo, bar]}

In [4]: d['bob'][0]() == d['bob'][1]()
Out[4]: False
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
dictionary keys are limited to hashable objects.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
I didn't mean the key itself
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
yeah, the values can be anything, but quite a lot of stuff can be keys.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
To be clear to the beginners, dictionary keys can be immutable objects like ints, floats, strings, and tuples. They can't be mutable objects like lists, dictionaries, or sets.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
for fun trivia: i made a little dictionary with keys of many different functions (references).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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