我怎样才能减少这种情况?
item 总是一个字符串
for item in list_of_items:
if ('beans' in item or 'apple' in item or 'eggs' in item or 'banana' in item) and ('elephant' not in item) or 'chicken' not in item:
print(item)
我的意思是我能以某种方式给出单词列表来检查每一种可能性吗?
请您参考如下方法:
您可以将 any 与生成器或列表理解一起使用:
if any(word in item for word in ['apple', 'beans', 'eggs', 'banana', 'elephant', 'chicken')):
