简体   繁体   English

如何检查输入是否在python中的列表中?

[英]How do I check if an input is in a list in python?

I want to be able to enter a value and check if it is in a list and if it is then in the list run a the rest of the program wanted. 我希望能够输入一个值并检查它是否在列表中,如果它在列表中运行,则运行所需的其余程序。

a=input('enter value')
b=(1,2,3,4)
c=(5,6,7,8,9)
if a is in b:
   print 'enter in code thats wanted'

You've written it yourself almost correctly, just instead of - 你自己写的几乎是正确的,而不是 -

if a is in b:

it should be - 它应该是 -

if a in b:

condition should be 条件应该是

if a in b:
    print 'enter in code thats wanted'
  • in operator: The 'in' operator is used to check if a value exists in a sequence or not. in运算符:'in'运算符用于检查序列中是否存在值。 Evaluates to true if it finds a variable in the specified sequence and false otherwise. 如果在指定序列中找到变量,则求值为true,否则求值为false。

  • 'is' operator: Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. 'is'运算符:如果运算符任一侧的变量指向同一对象,则求值为true,否则求值为false。

hence: 因此:

if a in b:
    print 'enter in the code that\'s wanted'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM