문제

I tried:

d = {3:'a',2:'b'}

if 'B' in d.values():
    print 'True'

For me B is equal to b, but I don't want change my dictionary.

It is possible test for case insensitive matches against the values of a dictionary?

How to check if 'B' is present in the dictionary without changing the values?

#

More complex:

d = {3:'A',2:'B',6:'c'}
도움이 되었습니까?

해결책

You'd have to loop through the values:

if any('B' == value.upper() for value in d.itervalues()):
    print 'Yup'

For Python 3, replace .itervalues() with .values(). This tests the minimum number of values; no intermediary list is created, and the any() loop terminates the moment a match is found.

Demo:

>>> d = {3:'a',2:'b'}
>>> if any('B' == value.upper() for value in d.itervalues()):
...     print 'Yup'
... 
Yup

다른 팁

if 'b' in map(str.lower, d.values()):
   ...
if filter(lambda x:d[x] == 'B', d):
  print "B is present
else:
  print "b is not present"

Try this ..

import sys

d = {3:'A',2:'B',6:'c'}
letter = (str(sys.argv[1])).lower()

if filter(lambda x : x == letter ,[x.lower() for x in d.itervalues()]):
    print "%s is present" %(letter)
else:
    print "%s is not present" %(letter)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top