如何在Python中查找列表或Dist中是否不包含值 [英] How to find whether a value is not contains in a List or Dist in Python

查看:313
本文介绍了如何在Python中查找列表或Dist中是否不包含值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 dist .__ contains__(value)来了解List或Dist中是否包含一个值,但是如果一个值需要返回True不包含在dist或list中。

我尝试了如果!dist ._ 包含_ (value)。显然,无效。请给我一个解决方案。

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) .Obviously, not worked.Please give me a solution.

推荐答案

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
>>> 

使用 not in 代替 in ,如果您要验证列表/字典中的值是否为 not

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

这篇关于如何在Python中查找列表或Dist中是否不包含值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆