我正在列出另一个列表中存在的元素列表。有两个条件:
条件1.需要完全匹配,因此我不使用“ if y in x”。
条件2.必须保留原始列表的顺序。
rhg_brands = ['Radisson Collection', 'Radisson Blu',
'Park Plaza', 'Radisson Red', 'Radisson']
brands_in_df = ['Radisson Collection',
'Park Plaza', 'Radisson Red', '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', 'Park Plaza', 'Radisson Red', 'Radisson', }
答案 0 :(得分:2)
您的“所需输出”是一组(大括号),但是您说您想要一个列表。因此,请按以下方式使用列表理解。
String joined = String.join(",", iteratable);