python“ in”将字符串与仅包含1个字符串的元组进行比较的操作
当将字符串与仅包含1个字符串的元组进行比较时,我注意到“在”操作中的奇怪行为。
'monday' in ('not monday')
结果是真的
好像我们要比较2个字符串,
但是如果我通过元组中添加另一个元素来更改表达式。
'monday' in ('not monday', 'not monday neither')
它按预期返回错误。
知道为什么吗?
I notice a strange behavior of "in" operation when comparing a string with a tuple containing only 1 string.
'monday' in ('not monday')
the result is True
as if we were comparing 2 strings
but if I change the expression by adding another element in the tuple.
'monday' in ('not monday', 'not monday neither')
it returns False as expected.
any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单元素元组必须具有尾随的逗号。否则,它将被解释为常规操作顺序括号,在这种情况下,
'星期一'在(“不是星期一”)
与'in in in Code>' “不是星期一”
。A single-element tuple must have the trailing comma. Otherwise, it gets interpreted as regular order-of-operations parentheses, which are meaningless in this case So,
'monday' in ('not monday')
is syntatically identical to'monday' in 'not monday'
.因为
('不是星期一')
并不意味着元组,这意味着字符串与'不是星期一'
一样写('不是星期一',)
(将,
添加到结尾)。祝你好运!
Because the
('not monday')
does not mean tuple, it means string as same as'not monday'
, if you want to create an only one unit tuple you can write('not monday',)
(add a,
to the end).good luck!