嗯……
我猜你charobj
可能是你自己实现的一个类。为了允许 Python 执行有意义的相等比较而不仅仅是盲比较,您必须重载默认方法,例如:
__eq__(self, other)
__gt__(self, other)
__lt__(self, other)
- ...
更多信息:https ://docs.python.org/3/reference/datamodel.html#special-method-names
无论如何,我做了一些测试,它适用于文字和内置类型。我在 Windows 10 (x64) 上使用 Python 2.7。
nr = 4
nc = 2
list = [('nc',nc,'test test test'),('nr',nr,'test test test')]
if ('nc', 2, 'test test test') in list:
print('OK')
else:
print('KO')
实际上打印OK
。
我试过了not in
,它打印出来KO
。
我还尝试用变量替换文字,它似乎也有效。
nr = 4
nc = 2
list = [('nc',nc,'test test test'),('nr',nr,'test test test')]
_nc = 'nc'
_message = 'test test test'
if (_nc, nc, _message) in list:
print('OK')
else:
print('KO')
也打印OK
。
希望有帮助。