4

我有 if 语句

if "1111111" in players or "0000000" in players or "blablabla" in players:
    do something

简而言之怎么写?

4

3 回答 3

4

您可以使用any()

things = ["1111111", "0000000", "blablabla"]

if any(thing in players for thing in things):
于 2013-04-14T06:51:39.790 回答
4
if any(x in players for x in ("1111111", "0000000", "blablabla")):
    # do something

如果您要进行大量此类成员资格检查,您可以考虑将其制作playersset不需要潜在地遍历每次检查的整个序列。

于 2013-04-14T06:52:18.483 回答
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
于 2013-04-14T06:51:52.927 回答