我想在 python 中按名称忽略 ignore_list 中的所有项目。例如考虑
fruit_list = ["apple", "mango", "strawberry", "cherry", "peach","peach pie"]
allergy_list = ["cherry", "peach"]
good_list = [f for f in fruit_list if (f.lower() not in allergy_list)]
print good_list
我希望 good_list 也忽略“桃子派”,因为桃子在过敏列表中并且桃子派包含桃子:-P
最佳答案
怎么样:
fruits = ["apple", "mango", "strawberry", "cherry", "peach","peach pie"]
allergies = ["cherry", "peach"]
okay = [fruit for fruit in fruits if not any(allergy in fruit.split() for allergy in allergies)]
# ['apple', 'mango', 'strawberry']
关于python - 在python中按名称忽略忽略列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709045/