在列表中查找项目 [英] Finding an item in a list

查看:63
本文介绍了在列表中查找项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to find an item in a list in python. Here is my function:

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

    for i in operatorlist:
        if input is operatorlist[i]:
            return True

我的代码坏了,我不知道为什么...有什么想法?

My code is breaking, and I can't figure out why... any ideas?

我从以下位置更改了代码

I changed my code from:

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

因为有人告诉我,从本质上说,这样做是愚蠢的.

Because I was told that is was, essentially, stylistically dumb to write it that way.

推荐答案

一行:

return input in operatorlist

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

No need to do conditions here, the in operator already returns a boolean.

这篇关于在列表中查找项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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