标签: python if-statement for-loop
我可以这样做:
s="j g a c" for a in ("a","b","c"): if a in s: print("asd") break
像这样:
if s in a: print("asd")
还是以其他方式少写?
答案 0 :(得分:2)
不,当然你不能这样做;是"j g a c" in ("a","b","c")?
"j g a c" in ("a","b","c")
但是,您可以使用any来缩短代码:
any
s = "j g a c" if any(a in s for a in "abc"): print("asd")
答案 1 :(得分:1)
您可以使用set。
set
if set(s).intersection('abc'): print('asd')