简体   繁体   English

从字典中检索键和值

[英]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. 在此之后,我要求用户输入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() . 如果您使用的是Python 3.x,则将raw_input()替换为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] ). 如果用户输入“整数”,则可以显式索引到字典中(即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 . 至于另一种方式,如果您想创建字典的反向映射(我认为您是这样做的),那么最好看看这个SO问答 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 检查此站点上的“词典”部分以获取文档: http : //docs.python.org/tutorial/datastructures.html

streetno.keys() delivers the keys and you can iterate over the keys to get the values: streetno.keys()提供密钥,您可以遍历密钥以获取值:

pseudo-code: 伪代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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