我有if声明
if "1111111" in players or "0000000" in players or "blablabla" in players:
do something
如何简写?
答案 0 :(得分:4)
您可以使用any()
:
things = ["1111111", "0000000", "blablabla"]
if any(thing in players for thing in things):
答案 1 :(得分:4)
if any(x in players for x in ("1111111", "0000000", "blablabla")):
# do something
如果您要进行大量这些成员资格检查,您可以考虑将players
变为set
,而{{1}}不需要在每次检查时遍历整个序列。
答案 2 :(得分:2)
if any(a in players for a in list_of_tests):
#something
pass
如果您使用numpy.any()
,则需要先将生成器转换为列表。
if np.any([a in players for a in list_of_tests]):
#something
pass