我想找到一种更优雅的编码方式:
str_example = "u are my // path"
if '//' in str_example or '/' in str_example:
do something
答案 0 :(得分:5)
使用all()
和any()
函数
str_example = "u are my // path"
if any(s in str_example for s in ['//', '/']):
pass
答案 1 :(得分:3)
您可以将子字符串存储在数组中,并使用类似:
needles = ['//', '/']
if any(needle in str_example for needle in needles):
do something
答案 2 :(得分:1)
如果要检查的内容只有两个(可能是三个),我可能会将代码保持原样。如果可以用一小段非常简单的代码表达某些内容,那为什么不这样做呢?
四个(可能三个)或更多,我会使用其他几个已经建议的any(...)
变体。