使列表与另一个列表python完全匹配

前端之家收集整理的这篇文章主要介绍了使列表与另一个列表python完全匹配 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在列出另一个列表中存在的元素列表.有两个条件:

条件1.需要完全匹配,因此我不使用“如果x中的y”.

条件2.必须保留原始列表的顺序.

rhg_brands = ['Radisson Collection','Radisson Blu','Park Plaza','Radisson Red','Radisson']

brands_in_df = ['Radisson Collection','Radisson']

#remove brands from rhg_brands if they're not in the brands_in_df 
rhg_brands = set(rhg_brands).intersection(set(brands_in_df))  

#output:
{'Park Plaza','Radisson','Radisson Collection','Radisson Red'}

我希望输出以某种方式保留原始列表的顺序.

下面是所需输出的示例:

{'Radisson Collection',}
@H_403_24@最佳答案
您的“所需输出”是一组(花括号),但是您说您想要一个列表.因此,请按以下方式使用列表理解.

result = [x for x in rhg_brands if x in brands_in_df]

assert result==['Radisson Collection','Radisson' ]
原文链接:https://www.f2er.com/python/533150.html

猜你在找的Python相关文章