我想按名称忽略在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也忽略“ peach pie”,因为桃子在过敏列表中,而桃子饼中则包含桃子:-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/