TypeError: 'in ' requires string as left operand, not list (Function)

后端 未结 3 1293
既然无缘
既然无缘 2021-01-28 16:13
variableName=[\"display\",\"screen\",\"sound\"\"audio\"]
fileName=[\"PPP\", \"Abc\"]
P1=\"PPP\"
d=\"display\"
s=\"screen\"
ss=\"sound\"
a=\"audio\"
d=P1
loop=True
def CH         


        
相关标签:
3条回答
  • 2021-01-28 16:40

    I think you've got the logic of your in statement backwards. You should be checking

    if Up.lower() in variableName:
    

    not the other way around.

    0 讨论(0)
  • 2021-01-28 16:48

    Using

    if (variableName) in Up.lower():
    

    ....tests for the existence of a tuple (variableName) in a string Up.lower().

    Since variableName is actually a list of strings, depending on what you are trying to achieve, you may well want to use one of the following instead:

    if any(s in Up.lower() for s in variableName):
    
    if all(s in Up.lower() for s in variableName):
    
    • The any() option will return True if any of the strings s in variableName are in Up.lower().
    • The all option will return True if all of the strings s in variableName are in Up.lower().
    0 讨论(0)
  • 2021-01-28 16:49

    If variableName is a list, you can check membership with .__contains__().

    Try changing the if statement to if variableName.__contains__(Up.lower):

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