如果值x ==列表y中的任何值:python

use*_*220 0 python if-statement

我是python的新手,想做类似的事情:

if value x == any value in list y:
   write x
Run Code Online (Sandbox Code Playgroud)

这将与函数一起运行,因此只有符合某个标准的解决方案才会在生成时写入csv

pok*_*oke 7

>>> x = 8
>>> if x in (1, 2, 3, 5, 8, 13, 21, 34, 55):
        print('x is an early Fibonacci number')
x is an early Fibonacci number
Run Code Online (Sandbox Code Playgroud)


izo*_*ica 5

您可以使用 in 语法:

if x in ["a", "b", "c"]
   ... do stuff
Run Code Online (Sandbox Code Playgroud)

而且,如果您关心 x 的出现次数,您可以使用计数:

if y.count(x) == 1
  ... do stuff
Run Code Online (Sandbox Code Playgroud)