【问题标题】:How to remove all duplicate elements from a list of lists in Python while also deleting the orignals如何从 Python 列表中删除所有重复元素,同时删除原始元素
【发布时间】:2021-06-16 13:27:40
【问题描述】:

我正在尝试比较许多不同波形的傅立叶变换之间的差异。每个变换中最突出的峰值(单个频率)位于单个列表中。我想在所有列表中找到共同的峰值/元素并完全删除它们。例如:

输入:

List1=[1,2,3,4,5,6,11]
List2=[2,3,7,8,9]
List3=[9,1,8,5,10,12]
ListofListIn=[List1,List2,List3]

输出:

List1=[4,6,11]
List2=[7]
List3=[10,12]
ListofListOut=[List1,List2,List3]

我需要它来处理任意数量的任意大小的列表。我可能有 50 多个不同大小的列表。只要单独列表的元素不混合,我不关心单独列表中的顺序。

我已经研究了set.intersection,但我的理解是它只会将一组与另一组进行比较,如果我有未知数量的列表,更不用说做我想要的每组都需要相交变得非常困难每隔一组 50*50=2500 这是许多交叉点。同样set.intersection 只删除重复项而不是原始项。

我希望我正在尝试做的事情对你们那里的 Python 向导有意义。我已经在这里撞了几个小时的砖墙了,如果提供任何帮助,我将不胜感激。

【问题讨论】:

  • 将所有元素放入collections.Counter,然后过滤掉所有计数大于1的元素。比尝试做N^2个交集要容易得多;它与元素总数成线性关系。

标签: python list duplicates set intersection


【解决方案1】:

使用collections.Counter

>>> List1=[1,2,3,4,5,6,11]
>>> List2=[2,3,7,8,9]
>>> List3=[9,1,8,5,10,12]
>>> from collections import Counter
>>> c = Counter(i for a in (List1, List2, List3) for i in a)
>>> [i for i in List1 if c[i] == 1]
[4, 6, 11]
>>> [i for i in List2 if c[i] == 1]
[7]
>>> [i for i in List3 if c[i] == 1]
[10, 12]

【讨论】:

  • 非常令人印象深刻的方法
  • 这非常有效,我只需要进行一些小的调整就可以完全按照我的意愿工作,你真的是一个聪明的山姆。
【解决方案2】:
In [33]: List1=[1,2,3,4,5,6,11] 
    ...: List2=[2,3,7,8,9] 
    ...: List3=[9,1,8,5,10,12]                                                                                                                                                                                                                                                

In [34]: counts = collections.Counter([*List1, *List2, *List3])                                                                                                                                                                                                               

In [35]: uniques = {c for c,count in counts.items() if count==1}                                                                                                                                                                                                              

In [36]: uniques                                                                                                                                                                                                                                                              
Out[36]: {4, 6, 7, 10, 11, 12}

In [37]: List1 = sorted(uniques.intersection(List1), key=List1.index)                                                                                                                                                                                                         

In [38]: List1                                                                                                                                                                                                                                                                
Out[38]: [4, 6, 11]

In [39]: List2 = sorted(uniques.intersection(List2), key=List2.index)                                                                                                                                                                                                         

In [40]: List2                                                                                                                                                                                                                                                                
Out[40]: [7]

In [41]: List3 = sorted(uniques.intersection(List3), key=List3.index)                                                                                                                                                                                                         

In [42]: List3                                                                                                                                                                                                                                                                
Out[42]: [10, 12]

In [43]: ListofListOut=[List1,List2,List3]                                                                                                                                                                                                                                    

In [44]: ListofListOut                                                                                                                                                                                                                                                        
Out[44]: [[4, 6, 11], [7], [10, 12]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多