Python IF multiple “and” “or” in one statement

前端 未结 1 519
感动是毒
感动是毒 2020-12-19 10:02

I am just wondering if this following if statement works:

    value=[1,2,3,4,5,f]
    target = [1,2,3,4,5,6,f]
    if value[0] in target OR value[1] in targe         


        
相关标签:
1条回答
  • 2020-12-19 10:46

    Use parenthesis to group the conditions:

    if value[6] in target and (value[0] in target or value[1] in target):
    

    Note that you can make the in lookups in constant time if you would define the target as a set:

    target = {1,2,3,4,5,6,f}
    

    And, as mentioned by @Pramod in comments, in this case value[6] would result in an IndexError since there are only 6 elements defined in value and indexing is 0-based.

    0 讨论(0)
提交回复
热议问题