查找列表中的项目

gfp*_*ste 0 python parsing list

所以我试图在python的列表中找到一个项目.这是我的功能:

def operator(input):
    operatorlist = ['+', '-', '*', '/', '^', 'sin', 'cos']

    for i in operatorlist:
        if input is operatorlist[i]:
            return True
Run Code Online (Sandbox Code Playgroud)

我的代码破了,我无法弄清楚为什么......任何想法?

我改变了我的代码:

def operator(input):
    if input is '+' or input is '-' or input is '*' or input is '/' or input is '^' or input is 'sin' or input is 'cos':
    return True
Run Code Online (Sandbox Code Playgroud)

因为我被告知,基本上,风格上是愚蠢的,以这种方式写它.

Cha*_*guy 11

一条线 :

return input in operatorlist
Run Code Online (Sandbox Code Playgroud)

这里不需要做条件,in运算符已经返回一个布尔值.