We would like to perform a conditional branch using a list in the python for loop statement.Specifically,
li = [2,4,6,9]
for i in range (100):
if i!=2 and i!=4 and i!=6 and i!=9:
"I would like to express ""2,"" ""4,"" ""6,"" and ""9"" in the if statement above using the list li."
It doesn't have to be written like above
Thank you for your cooperation.
You can use not in .
The operators in and not in look at membership, where x is s
The element in is true, otherwise false, and x notins returns xins negation.
in not in li
If you want to write in the for sentence, the iterator is better.
for in (x for x in range(100) if x not in li):
Or
for i filter (lambdax:x not in li, range(100)):
A1
li = [2,4,6,9]
for i in range (100):
if not in li:
# Implement what you want to do here
A2
li = [2,4,6,9]
for in [ x for x in range(100) if x not in li ]:
# Implement what you want to do here
© 2024 OneMinuteCode. All rights reserved.