简体   繁体   中英

Contains() function `in` in if condition in python

I did some research about functionally of contains() in especially in comparison with eq() == and found out that it can perform many tasks. I managed to answer many of them (see below). Is there any other useful usage of in except those below, for example with objects?

I am also curious about situation mentioned in Python's "in" set operator that b in s mean that there is an element x of the set s such that x == b and hash(x) == hash(b). How can it be otherwise? Is there an example where both are not equal?

Research about in: in produces the same result in case of comparison of a string of the length of 1.

data = ['2','4','1','3']

for d in data:
 if '1' in d:
  print(d)
  print(data.index(d))

for d in data:
 if d in '1':
  print(d)
  print(data.index(d))

for d in data:
 if '1' == d:
  print(d)
  print(data.index(d))

all 3 produces following result:

1
2

Though here the similarity ends. In can be used for broad range of other comparisons:

data = [['1','2'],'4','1','3']

for d in data:
 if '1' in d:
  print(d)
   print(data.index(d))

>> ['1', '2']
>> 0
>> 1
>> 2

But in this case it is order sensitive:

for d in data:
 if d in '1':
  print(d)
  print(data.index(d))

>> TypeError: 'in <string>' requires string as left operand, not list

You can check the original list directly, but it works for sets, tuples, dict keys and strings.

data = ['3','2','1'] #string in list, work for numbers, lists etc.
if '1' in data:
 print(data.index('1'))
>> 2

data = ['3',['2','1'],'0'] #string in list in list
if '1' in data:
 print(data.index('1'))
else:
 print('not found')
>> not found

data = ['3','x',['2','1'],'0'] #list in list
if ['2','1'] in data:
 print(data.index(['2','1']))
else:
 print('not found')
>> 2

data = ('3','2','1') #string in tuple
if '1' in data:
 print(data.index('1'))
>> 2

data = set(['3','2','1']) #string in set
 if '1' in data:
 print('ok')
>> ok

data = {'1':'a','2':'b'} #string in dict keys
if '1' in data:
 print(data['1'])
>> a

data = {'a':'1','b':'2'} #string dict values
if '1' in data:
 print('ok')
>>

data = 'abc1efg' #string in string 
if '1' in data:
 print(data.index('1')) 
>> 3

data = 'abc1efg' #number in string 
if 1 in data:
 print(data.index(1)) 
>> TypeError: 'in <string>' requires string as left operand, not int 

data = [1,'x',(),{}] #dict in list
if {} in data:
 print(data.index({}))
>> 3

All of the above works with function contains from operator module.

import operator
data = [1,'x',(),{}] #contains function
if operator.contains(data,{}):
 print(data.index({}))
>> 3

You can both define equality and hash in python, in this class both are not equal.

class EvilClass:
    def __eq__(self, item):
        return False
    def __hash__(self):
        return 1

a = EvilClass()
b = EvilClass()
print hash(a) == hash(b) # True
print a == b # False

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