我用Python写了一个脚本来解析一些字符串。
问题是我需要检查字符串是否包含某些部分,但我找到的方法不够聪明。
以下是我的代码:
if ("CondA" not in message) or ("CondB" not in message) or ("CondC" not in message) or ...:
有没有优化这个的方法?我还有6个其他检查需要进行此条件。
我用Python写了一个脚本来解析一些字符串。
问题是我需要检查字符串是否包含某些部分,但我找到的方法不够聪明。
以下是我的代码:
if ("CondA" not in message) or ("CondB" not in message) or ("CondC" not in message) or ...:
any
函数:if any(c not in message for c in ("CondA", "CondB", "CondC")):
...
使用带有any()
或all()
的生成器:
if any(c not in message for c in ('CondA', 'CondB', ...)):
...
map()
的惰性特性:if not all(map(message.__contains__, ('CondA', 'CondB', ...))):