python列表支持in运算符吗,如何使用Python的“ in”运算符检查我的列表/元组是否包含整数0、1、2?...

How do I use the Python in operator to check my list/tuple sltn contains each of the integers 0, 1, and 2?

I tried the following, why are they both wrong:

# Approach 1

if ("0","1","2") in sltn:

kwd1 = True

# Approach 2

if any(item in sltn for item in ("0", "1", "2")):

kwd1 = True

Update: why did I have to convert ("0", "1", "2") into either the tuple (1, 2, 3)? or the list [1, 2, 3]?

解决方案if ("0","1","2") in sltn

You are trying to check whether the sltn list contains the tuple ("0","1","2"), which it does not. (It contains 3 integers)

But you can get it done using #all() :

sltn = [1, 2, 3] # list

tab = ("1", "2", "3") # tuple

print(all(int(el) in sltn for el in tab)) # True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值