问题:如何检查字典中是否存在值(Python)

我在python中有以下字典:

d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}

我需要一种方法来查找此字典中是否存在诸如“一个”或“两个”的值。

例如,如果我想知道索引“ 1”是否存在,我只需要键入:

"1" in d

然后python会告诉我这是对还是错,但是我需要做同样的事情,只是要找到一个值是否存在。

I have the following dictionary in python:

d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}

I need a way to find if a value such as “one” or “two” exists in this dictionary.

For example, if I wanted to know if the index “1” existed I would simply have to type:

"1" in d

And then python would tell me if that is true or false, however I need to do that same exact thing except to find if a value exists.


回答 0

>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
>>> 'one' in d.values()
True

出于好奇,一些比较时机:

>>> T(lambda : 'one' in d.itervalues()).repeat()
[0.28107285499572754, 0.29107213020324707, 0.27941107749938965]
>>> T(lambda : 'one' in d.values()).repeat()
[0.38303399085998535, 0.37257885932922363, 0.37096405029296875]
>>> T(lambda : 'one' in d.viewvalues()).repeat()
[0.32004380226135254, 0.31716084480285645, 0.3171098232269287]

编辑:并且,如果您想知道为什么…原因是上述每种方法返回的对象类型不同,则该对象可能适合或可能不适合于查找操作:

>>> type(d.viewvalues())
<type 'dict_values'>
>>> type(d.values())
<type 'list'>
>>> type(d.itervalues())
<type 'dictionary-valueiterator'>

EDIT2:根据注释中的请求…

>>> T(lambda : 'four' in d.itervalues()).repeat()
[0.41178202629089355, 0.3959040641784668, 0.3970959186553955]
>>> T(lambda : 'four' in d.values()).repeat()
[0.4631338119506836, 0.43541407585144043, 0.4359898567199707]
>>> T(lambda : 'four' in d.viewvalues()).repeat()
[0.43414998054504395, 0.4213531017303467, 0.41684913635253906]
>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
>>> 'one' in d.values()
True

Out of curiosity, some comparative timing:

>>> T(lambda : 'one' in d.itervalues()).repeat()
[0.28107285499572754, 0.29107213020324707, 0.27941107749938965]
>>> T(lambda : 'one' in d.values()).repeat()
[0.38303399085998535, 0.37257885932922363, 0.37096405029296875]
>>> T(lambda : 'one' in d.viewvalues()).repeat()
[0.32004380226135254, 0.31716084480285645, 0.3171098232269287]

EDIT: And in case you wonder why… the reason is that each of the above returns a different type of object, which may or may not be well suited for lookup operations:

>>> type(d.viewvalues())
<type 'dict_values'>
>>> type(d.values())
<type 'list'>
>>> type(d.itervalues())
<type 'dictionary-valueiterator'>

EDIT2: As per request in comments…

>>> T(lambda : 'four' in d.itervalues()).repeat()
[0.41178202629089355, 0.3959040641784668, 0.3970959186553955]
>>> T(lambda : 'four' in d.values()).repeat()
[0.4631338119506836, 0.43541407585144043, 0.4359898567199707]
>>> T(lambda : 'four' in d.viewvalues()).repeat()
[0.43414998054504395, 0.4213531017303467, 0.41684913635253906]

回答 1

您可以使用

"one" in d.itervalues()

测试是否"one"在字典的值中。

You can use

"one" in d.itervalues()

to test if "one" is among the values of your dictionary.


回答 2

Python字典具有get(key)函数

>>> d.get(key)

例如,

>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
>>> d.get('3')
'three'
>>> d.get('10')
none

如果您的密钥不存在,将返回none值。

foo = d[key] # raise error if key doesn't exist
foo = d.get(key) # return none if key doesn't exist

与小于3.0和大于5.0的版本相关的内容。。

Python dictionary has get(key) funcion

>>> d.get(key)

For Example,

>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
>>> d.get('3')
'three'
>>> d.get('10')
none

If your key does’nt exist, will return none value.

foo = d[key] # raise error if key doesn't exist
foo = d.get(key) # return none if key doesn't exist

Content relevant to versions less than 3.0 and greater than 5.0. .


回答 3

使用字典视图:

if x in d.viewvalues():
    dosomething()..

Use dictionary views:

if x in d.viewvalues():
    dosomething()..

回答 4

不同类型检查值是否存在

d = {"key1":"value1", "key2":"value2"}
"value10" in d.values() 
>> False

如果值列表怎么办

test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6']}
"value4" in [x for v in test.values() for x in v]
>>True

如果带有字符串值的值列表怎么办

test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6'], 'key5':'value10'}
values = test.values()
"value10" in [x for v in test.values() for x in v] or 'value10' in values
>>True

Different types to check the values exists

d = {"key1":"value1", "key2":"value2"}
"value10" in d.values() 
>> False

What if list of values

test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6']}
"value4" in [x for v in test.values() for x in v]
>>True

What if list of values with string values

test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6'], 'key5':'value10'}
values = test.values()
"value10" in [x for v in test.values() for x in v] or 'value10' in values
>>True

回答 5

在Python 3中,您可以使用values()字典的功能。它返回值的视图对象。这又可以传递给iter返回迭代器对象的函数。可以使用来检查迭代器in,如下所示:

'one' in iter(d.values())

或者您可以直接使用视图对象,因为它类似于列表

'one' in d.values()

In Python 3 you can use the values() function of the dictionary. It returns a view object of the values. This, in turn, can be passed to the iter function which returns an iterator object. The iterator can be checked using in, like this,

'one' in iter(d.values())

Or you can use the view object directly since it is similar to a list

'one' in d.values()

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。