开发者

How to find whether a value is not contains in a List or Dist in Python

I can use dist.__ contains__ (value) to know whether a value is included or not in a List or Dist.But I need to return True if a value is not included in dist or list.

I tried If !dist._contains _(value) .Obvio开发者_如何学Gously, not worked.Please give me a solution.


It's simple:

if value not in dist:


Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
>>> 3 in [1, 2, 3] # To know if a value is in a list
True
>>> 3 in {1: 'one', 2: 'two', 3: 'three'} # To know if a value is in keys of a dict
True
>>> 'three' in {1: 'one', 2: 'two', 3: 'three'}.values() # To know if a value is in values of a dict
True
>>> 

Use not in instead of in if you want to verify if a value is not in a list/dict.


Unfortunately, I do not know what a dist is, but with lists this works:

>>> 5 not in [1,2,3,4,5]
False
>>> 6 not in [1,2,3,4,5]
True
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜