python - 检查以确保字符串不包含多个值

标签 python algorithm string

**注意 - 我不会只在字符串的末尾进行测试 - 需要在字符串的任何位置找到特定的子字符串

检查以确保字符串不包含多个值的最快方法是什么。我当前的方法效率低下且不符合 pythonic:

if string.find('png') ==-1 and sring.find('jpg') ==-1 and string.find('gif') == -1 and string.find('YouTube') == -1:

最佳答案

尝试:

if not any(extension in string for extension in ('jpg', 'png', 'gif')):

这与您的代码基本相同,但编写得更优雅。

关于python - 检查以确保字符串不包含多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542801/

相关文章:

python - 包含 NaN python 的数据集中的 argmin

python - 在 python 3.4 中使用正则表达式的语法无效

python - 找不到 GraphViz 的可执行文件(python 3 和 pydotplus)

algorithm - 是否可以在图像和星座之间建立关联?

java - 高效的 10 次方 x 算法

python - pytorch 变量索引丢失一维

algorithm - 在给定的欧氏距离内的二维空间中查找移动节点对?

java - 帮助更好地解析 Java 中字符串中的数字

c++ - 检索字符串的第一个字符并在 C++ 中按字母顺序比较它

string - 来自一袋字符串的超序列