While studying Python scraping, when I was processing values, I got TypeError:'in <string>' requires string as left operand, not list
, so I will leave a countermeasure in the memo
I was trying to do a conditional branch to see if the corresponding string was found in the list obtained by Python.
if 'The character string you want to apply' in i:
Then I got this error
TypeError: 'in <string>' requires string as left operand, not list
Use list comprehension
False not in [i in 'The character string you want to apply'for i in A list containing the strings you want to search for]
Quoted from @ shiracamus's comment! (Thank you!)
If you just want to make a judgment, you can use any function or all function.
>>> any('test' in item for item in ['hoge', 'fuge', 'hogetestfuge'])
True
>>> all('test' in item for item in ['hoge', 'fuge', 'hogetestfuge'])
False
>>> all('test' in item for item in ['testhoge', 'fugetest', 'hogetestfuge'])
True
reference https://pg-chain.com/python-in https://ai-inter1.com/python-if-in/ https://kuzunoha-ne.hateblo.jp/entry/2019/02/15/213000
Recommended Posts