Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有代码
if "a" in ("abc")
它返回真。我怎样才能做到这一点,只有
if "abc" in ("abc")
返回 true 而不使用for循环而不是第一个示例?
for
这里的问题是
("abc")
不是元组。Python 使用括号进行分组以及元组构造(严格来说,逗号是元组构造运算符)。它必须决定这是一个元组还是只是分组,它必须选择分组。要修复它,请使用
if "a" in ("abc",)
注意逗号。