简体   繁体   中英

Retrieving Key and Values from Dictionary

I used a Dictionary as shown below:

streetno={
    "1":"Sachin Tendulkar",
    "2":"Sehawag",
    "3":"Dravid",
    "4":"Dhoni",
    "5":"Kohli"
}      

After this I'm asking user for i/p. Then, depending upon the user input I want to retrieve either key or value from the dictionary. How can I implement this?? Which method I can use to implement this??

I may be misunderstanding this, but it looks like you may be asking a user to give you either a key or a value. If they give you a key you want to return the value and if they give you a value you want to return the key.

If that is what you are looking for, the easiest way to do this is to add entries to the dictionary with the values and keys swapped, for example:

streetno={"1":"Sachin Tendulkar","2":"Sehawag","3":"Dravid","4":"Dhoni","5":"Kohli"}
streetno.update([(v, k) for k, v in streetno.items()])

This results in the following dictionary:

>>> pprint(streetno)
{'1': 'Sachin Tendulkar',
 '2': 'Sehawag',
 '3': 'Dravid',
 '4': 'Dhoni',
 '5': 'Kohli',
 'Dhoni': '4',
 'Dravid': '3',
 'Kohli': '5',
 'Sachin Tendulkar': '1',
 'Sehawag': '2'}

With this you can get the input and look up the value in the dictionary without any additional checking:

key = raw_input("Enter name or number (i/p):")
result = streetno.get(key)

If you are using Python 3.x, replace raw_input() with input() .

You could create an auxiliary dictionary with the reverse mappings:

In [4]: dict((v,k) for k,v in streetno.items())
Out[4]: 
{'Dhoni': '4',
 'Dravid': '3',
 'Kohli': '5',
 'Sachin Tendulkar': '1',
 'Sehawag': '2'}

Once you know which way you need to map, you either use the original or the auxiliary dictionary for the lookup.

If the user inputs an "integer", you can explicitly index into the dictionary (ie streetno[value] ). Your keys appear to be strings, so you can get away with not having to cast them as integers or do anything to get around the issue that it could be a string or integer. If your keys were integers, then you would have to do a bit of casting and finagling - which I leave as an exercise to you.

As for the other way 'round, if you want to create a reverse mapping of the dictionary (which I think you do), then you'd be best to take a look at this SO question and answer . You would have to create a separate dictionary for it, and use that method to map the key values backwards.

A small code sample to get you started...

streetno =
    {
     "1":"Sachin Tendulkar",
     "2":"Sehawag",
     "3":"Dravid",
     "4":"Dhoni",
     "5":"Kohli"
     }

input=raw_input()
try:
    print streetno[input]
except KeyError:
    print [k for k, v in streetno.items() if v == input][0]

Check the "Dictionaries" section on this site for documentation: http://docs.python.org/tutorial/datastructures.html

streetno.keys() delivers the keys and you can iterate over the keys to get the values:

pseudo-code:

foreach key in streetno.keys()
            print "key: ", key, ", value: ", streetno[ key ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM