Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表理解代替嵌套循环避免项目重复_Python_List Comprehension_Unique_Nested Loops - Fatal编程技术网

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

Python 列表理解代替嵌套循环避免项目重复,python,list-comprehension,unique,nested-loops,Python,List Comprehension,Unique,Nested Loops,我有一些函数可以检索项目列表、ITEN矩阵。 检索矩阵后,我需要将iten放入一个新列表中,并避免重复。 使用嵌套for循环很容易,但我想知道如何在列表理解中执行相同的操作。 我的问题是设置条件以避免插入重复的: 大概是这样的: 伪代码: new= [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans',

我有一些函数可以检索项目列表、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']

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

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

为什么不直接使用
set
{val for sublist in new for val in sublist}
?我不知道。谢谢,真的很简单。我没有提到,但是新列表是动态创建的,所以它可以保存更多的记录。。。因此,列表(set((item for list_uuuin new for item in list_uuu))转换是最好的选择。谢谢。新列表是动态的,排序一点也不重要。您的第二种方法非常适合。谢谢。