python - 使用 'in' 运算符的多值检查 (Python)

标签 python

if 'string1' in line: ...

... 按预期工作,但如果我需要像这样检查多个字符串怎么办:

if 'string1' or 'string2' or 'string3' in line: ...

...似乎不起作用。

最佳答案

if any(s in line for s in ('string1', 'string2', ...)):

关于python - 使用 'in' 运算符的多值检查 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2308944/

相关文章:

python - 计算对数,为什么这个算法效率不高,如何提高效率?

python - 如果 Python Pandas 中的索引相互匹配,则减去日期

Python - 使用 BeautifulSoup 在页面中抓取多个类

python - pip install中 "Failed building wheel for X"是什么意思?

python - 将 MATLAB quadgk 集成转换为具有长函数的 Scipy quad 以进行集成(传递 lambda 函数的任何替代方法?)

python - 使用 python 读取 csv 文件让我到达最后一行

python - 高效计算 Khatri-Rao 类总和(成对行总和)

python - Matplotlib/python 可点击点

python - 将数组的字符串表示形式转换为 python 中的 numpy 数组

python - 为什么我会收到 python ImportError : No module named html_parser?