列表理解代替嵌套循环避免项目重复

2024-05-14 07:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些函数可以检索项目列表、ITEN矩阵。 检索矩阵后,我需要将iten放入一个新列表中,并避免重复。 使用嵌套for循环很容易,但我想知道如何在列表理解中执行相同的操作。 我的问题是设置条件以避免插入重复的: 大概是这样的:

伪代码:

new= [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]

lista2 =[]
for movieL in new:
        lista2 = [val
                for sublist in new
                for val in sublist
              #if val not in lista2 this does not work
]

结果:

['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2', 'Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']

Tags: ofthein列表newfor矩阵val
3条回答

如果保留原始顺序在结果中并不重要,则可以利用集合,并使用集合并集操作:

from functools import reduce


new= [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
result = [*reduce(set.union, map(set, new))]
print(result)
#Outputs ['Captain Marvel', 'Venom', 'Black Panther', 'Ant-Man And The Wasp', 'American Assassin', 'Inhumans', 'Deadpool 2', 'Avengers: Infinity War', 'The Fate Of The Furious']

或者,如果您严格需要使用理解语法(在本例中为生成器理解),您可以使用:

result = [*set(item for list_ in new for item in list_)]

如果排序很重要:

new = [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
lst = [item for sublist in new for item in sublist]
print(sorted(list(set(lst)), key=lambda x: lst.index(x)))

如果没有关系:

new = [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
print(list(set([item for sublist in new for item in sublist])))

您可以使用itertools.chain.from_iterable()将所有列表展平为一个列表,并应用set()删除重复列表

from itertools import chain
new= [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
set(chain.from_iterable(new))


{'Captain Marvel', 'Avengers: Infinity War', 'The Fate Of The Furious', 'Inhumans', 'Ant-Man And The Wasp', 'Black Panther', 'Deadpool 2', 'Venom', 'American Assassin'}

相关问题 更多 >

    热门问题