2011-07-20 52 views
0

我可以使用dist.__ contains__ (value)來了解某個值是否包含在List或Dist中,但是如果未包含值,則需要返回True在dist或list中。我試過If !dist._contains _(value)。很明顯,沒有成功。如何查找某個值是否包含在Python中的List或Dist中

+0

我正在使用,除非關鍵字Ruby.Can我在Python中使用? – shajin

+1

你的意思是'字典'?如果是這樣,請修正您的問題,正確拼寫'dict'。成千上萬的人可以看到(並從中吸取)這個問題。 –

回答

2
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,如果你想驗證列表/字典中的值是否爲而不是

4

很簡單:

if value not in dist: 
+0

昨天晚上開始學習python。這就是爲什麼。 :) nyway謝謝 – shajin

+0

@shajin:接受你將要使用的答案。 「謝謝」很好。點擊接受標記才能使Stack Overflow正常工作。 –

1

不幸的是,我不知道DIST是什麼,但列出了此作品:

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

對不起,我的意思是字典(字典).nyway我得到了答案。謝謝 – shajin

相關問題