列表理解代替嵌套循环避免项目口是心非

list comprehension in place of nested loop avoiding item duplicity

我有一些函数可以检索项目列表、项目矩阵。 检索矩阵后,我需要将 itens 放在一个新列表中,并避免重复的。 使用嵌套的 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']

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

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_)]

您可以使用 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'}

如果排序很重要:

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])))