Python 返回在另一个列表中找到的项目列表

Python 返回在另一个列表中找到的项目列表,python,Python,我有一份品牌清单: brands = ['Nike', 'Reebok'] 以及产品清单: products = ['Nike Air Max', 'Sony PS5', 'Samsung Galaxy S20', 'Apple Iphone 12', 'Air Jordan'] 我需要检查在产品列表中是否找到品牌列表中的任何品牌,然后返回找到的新品牌列表 我使用了来自的代码 但是,它会返回产品列表中的项目。如何更改代码以从品牌列表中返回品牌?只需翻转您正在筛选的列表以及使用any()搜索的

我有一份品牌清单:

brands = ['Nike', 'Reebok']
以及产品清单:

products = ['Nike Air Max', 'Sony PS5', 'Samsung Galaxy S20', 'Apple Iphone 12', 'Air Jordan']
我需要检查在产品列表中是否找到品牌列表中的任何品牌,然后返回找到的新品牌列表

我使用了来自的代码


但是,它会返回产品列表中的项目。如何更改代码以从品牌列表中返回品牌?

只需翻转您正在筛选的列表以及使用
any()搜索的列表即可

或者如果要使用
过滤器

brands_found = list(filter, lambda brand: any(brand in product for product in products))

请不要只发布代码作为答案,还要解释代码的作用以及它是如何解决问题的。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
brands_found = [brand for brand in brands if any(brand in product for product in products)]
brands_found = list(filter, lambda brand: any(brand in product for product in products))
brands_found = [item for item in brands if any(item in i for i in products)]