Python:无论CaSE为何,都检查值是否在列表中 [英] Python: check if value is in a list no matter the CaSE

查看:55
本文介绍了Python:无论CaSE为何,都检查值是否在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论字母大小写如何,我都想检查一个值是否在列表中,我需要有效地做到这一点.

I want to check if a value is in a list, no matter what the case of the letters are, and I need to do it efficiently.

这就是我所拥有的:

if val in list:

但是我希望它忽略大小写

But I want it to ignore case

推荐答案

check = "asdf"
checkLower = check.lower()

print any(checkLower == val.lower() for val in ["qwert", "AsDf"])

# prints true

使用 any()函数.此方法很好,因为您无需重新创建具有小写字母的列表,而是在列表上进行迭代,因此一旦找到真值,它将停止迭代并返回.

Using the any() function. This method is nice because you aren't recreating the list to have lowercase, it is iterating over the list, so once it finds a true value, it stops iterating and returns.

演示: http://codepad.org/dH5DSGLP

这篇关于Python:无论CaSE为何,都检查值是否在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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