4

I have if statement

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

How to write in short ?

3 Answers 3

4
if any(x in players for x in ("1111111", "0000000", "blablabla")): # do something 

If you are going to be doing a lot of these membership checks you may consider making players into a set which doesn't need to potentially traverse the entire sequence on each check.

Sign up to request clarification or add additional context in comments.

Comments

4

You can use any():

things = ["1111111", "0000000", "blablabla"] if any(thing in players for thing in things): 

Comments

2
if any(a in players for a in list_of_tests): #something pass 

If you are using numpy.any(), you will need to convert the generator to a list first.

if np.any([a in players for a in list_of_tests]): #something pass 

3 Comments

@Blender thanks, wasn't sure when I wrote it if any played nice with generators. I am used to using numpy.any which I am pretty sure does not.
I think numpy.any should work as well. All you're doing is iterating until the first False.
@Blender np.any (on 1.7) will return a generator if you pass it a generator. It makes assumptions about the input being array-like. Probably related to what you get if you do np.array(t for t in src) (which is an array with a single generator element).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.