【问题标题】:How to do union of several lists?如何做多个列表的联合?
【发布时间】:2018-01-02 15:09:18
【问题描述】:

我正在阅读这篇文章:Combining two lists and removing duplicates, without removing duplicates in original list,但我的需求不止于此。我至少有 30 个列表,我需要没有所有列表重复的联合。现在我的第一次尝试只是使用 + 将所有成员附加到一个大列表中,然后使用 set 删除重复项,但我不确定这是否是最佳解决方案:

编辑 - 添加示例:

list_a = ['abc','bcd','dcb']
list_b = ['abc','xyz','ASD']
list_c = ['AZD','bxd','qwe']
big_list = list_a + list_b + list_c
print list(set(big_list)) # Prints ['abc', 'qwe', 'bcd', 'xyz', 'dcb', 'ASD', 'bxd']

我真正的问题是,这是否是这种组合的最佳方式?

【问题讨论】:

  • 请在您的问题中添加一个示例。最好是三个列表,而不是三十个。
  • 只执行“合并两个列表”操作29次?

标签: python list


【解决方案1】:

如果我正确理解您要执行的操作,您可以使用带有任意数量的可迭代参数的 set.update 方法。

>>> lists = [[1,2,3], [3,4,5], [5,6,7]]
>>> result = set()
>>> result.update(*lists)
>>> 
>>> result
{1, 2, 3, 4, 5, 6, 7}

编辑:使用您的示例数据:

>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']
>>> 
>>> result = set()
>>> result.update(list_a, list_b, list_c)
>>> result
{'ASD', 'xyz', 'qwe', 'bxd', 'AZD', 'bcd', 'dcb', 'abc'}

【讨论】:

  • 这很好用,我唯一要添加的是使用print(list(result)),因为只有print(result) 在屏幕上给我set(['abc', 'qwe', 'bcd', 'xyz', 'dcb', 'ASD', 'bxd'])
【解决方案2】:

使用set.union(set1, set2, set3, ..)

>>> l1 = [1,2,3]
>>> l2 = [2,3,4]
>>> l3 = [3,4,5]
>>> set.union(*[set(x) for x in (l1, l2, l3)])
{1, 2, 3, 4, 5}

更紧凑(适用于 Py2 和 Py3,感谢@Lynn!):

>>> set.union(*map(set, (l1, l2, l3)))
set([1, 2, 3, 4, 5])

【讨论】:

  • 您的两个 sn-ps 实际上都适用于 Python 2 和 3。您的第二个 sn-p 缺少 )
【解决方案3】:

使用set.union 的一种方法有already been mentioned,尽管在首先将列表映射到set 实例之后应用到每个列表。

作为替代方案,可以省略显式的set 映射,因为set.union,很像set.updatethe accepted answer 中介绍的后一种方法)也采用任意数量的可迭代参数,允许直接调用@987654328 @ 在一个空集和提供的列表上。

>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']

>>> result = set().union(list_a, list_b, list_c)
>>> result
{'ASD', 'xyz', 'qwe', 'bxd', 'AZD', 'bcd', 'dcb', 'abc'}

【讨论】:

    【解决方案4】:

    你可以做的是创建一个函数来接受任意数量的列表,将它们展平并返回联合:

    from itertools import chain
    
    def union_lists(*iterables):
        union = []
        lookup = set()
    
        flattened = chain.from_iterable(iterables)
    
        for item in flattened:
            if item not in lookup:
                lookup.add(item)
                union.append(item)
    
        return union
    

    上述函数的好处是它保留了插入时列表项的顺序,不像set(),它是无序的。然而,它使用set() 来检查是否已添加项目,即O(1),但将它们插入到列表中,因为列表是有序的。

    它还使用itertools.chain.from_iterable 将列表展平,即O(n)

    然后您可以简单地在任意数量的列表上运行此函数:

    >>> list_a = ['abc','bcd','dcb']
    >>> list_b = ['abc','xyz','ASD']
    >>> list_c = ['AZD','bxd','qwe']
    >>> print(union_lists(list_a, list_b, list_c))
    ['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe']
    >>> list_d = ['bcd', 'AGF', 'def']
    >>> print(union_lists(list_a, list_b, list_c, list_d))
    ['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe', 'AGF', 'def']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2015-07-19
      • 2020-11-17
      • 1970-01-01
      • 2020-01-05
      相关资源
      最近更新 更多