gpt4 book ai didi

python - 为什么 python 'in' 运算符根据元组大小表现不同?

转载 作者:行者123 更新时间:2023-11-28 20:05:03 24 4
gpt4 key购买 nike

<分区>

在 python2 中:

>>> 'a' in ('ab')
True
>>> 'a' in ('ab', 'c')
False

如果我只是想测试某个字符串是否存在于给定的元组中,看起来我不能在元组大小为 1 时使用“in”运算符?有没有一致的方法来做到这一点?

更新:

谢谢大家。试过这个:

>>> tup='ab',
>>> type(tup)
<type 'tuple'>
>>> 'a' in tup
False

它解释了逗号可以很好地构成元组。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com