【问题标题】:list comprehension in place of nested loop avoiding item duplicity列表理解代替嵌套循环,避免项目重复
【发布时间】:2020-07-06 23:08:16
【问题描述】:

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

【问题讨论】:

  • 为什么不直接使用set:{val for sublist in new for val in sublist}
  • 我不知道。谢谢,真的很容易。

标签: python list-comprehension unique nested-loops


【解决方案1】:

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

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 是动态创建的,所以它可以保留更多的记录......所以, list(set( (item for list_ in new for item in list_)) 铸造是最好的选择。谢谢。
【解决方案2】:

如果排序很重要:

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

【讨论】:

  • 新列表是动态的,排序根本不重要。您的第二种方法非常适合。谢谢。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多