【发布时间】:2021-12-27 23:58:34
【问题描述】:
我需要合并 5 个列表,其中任何列表都可以为空,这样只有所有 5 个初始列表中的项目才会包含在新形成的列表中。
for filter in filters:
if filter == 'M':
filtered1 = [] # imagine that this is filled
if filter == 'V':
filtered2 = [] # imagine that this is filled
if filter == 'S':
filtered3 = [] # imagine that this is filled
if filter == 'O':
filtered4 = [] # imagine that this is filled
if filter == 'C':
filtered5 = [] # imagine that this is filled
filtered = [] # merge all 5 lists from above
所以现在我需要使用来自所有过滤列表 1-5 的合并数据来过滤列表。我该怎么做?
【问题讨论】:
-
这称为
intersection,通常在sets上执行,即无序数据没有重复。你能指定如何处理重复和排序吗? -
合并列表是否需要按任何特定顺序排列?
-
如果列表为空,我们是否“合并”?