【问题标题】:set operation on a list of elements对元素列表设置操作
【发布时间】:2018-12-02 17:47:54
【问题描述】:

我有一个包含数千个类似这样的集合的列表:

set_list = [a, b, c, d]

列表中的每个集合如下所示:

a = set([1, 2, 3, 4, 5])
b = set([4, 5, 6, 7, 7, 9])
c = set([1, 2, 6, 8, 10, 12, 45])
d = set([11, 3, 23, 3, 4, 44])

我想对列表中的每个集合执行集合操作:X-(YUZUAUB......etc),例如,这看起来像这样: 对set_list 中的所有元素应用此操作后,新元素如下所示:

a = a.difference(b.union(c, d))
b = b.difference(c.union(a, d))
c = c.difference(d.union(b, a))
d = d.difference(a.union(c, b))

我如何做到这一点?

【问题讨论】:

  • 你的“设置操作”不清楚。那些Us 是联合运营商吗? XYZ 是什么?
  • yes U: union 和 X,Y,A,B 等代表列表的元素

标签: python python-3.x loops set


【解决方案1】:

一种可能性是利用multiset module 预先计算set_list 中所有元素的多重集并集,如下所示:

from multiset import Multiset
union = sum(set_list, Multiset())
set_list = [s - (union - s) for s in set_list]

在这里,union - s 计算您的符号中的 Y ∪ Z ∪ A ∪ B...

请参阅Aran-Fey's answer,了解仅使用标准库实现(更详细)的相同方法。

【讨论】:

  • @ultron :我很好奇,为什么这是公认的答案?是因为处理大数据的速度最快,还是因为它是第一个发布的?尚未测量性能,但我的答案根本没有使用任何库......试图了解这里的原因
【解决方案2】:

如果我的理解正确,您需要每个集合的差异以及其余集合的并集。我会使用循环和functools.reduceoperator.or_

设置

import functools
import operator

a = set([1, 2, 3, 4, 5])
b = set([4, 5, 6, 7, 7, 9])
c = set([1, 2, 6, 8, 10, 12, 45])
d = set([11, 3, 23, 3, 4, 44])
set_list = [a, b, c, d]

循环并保存结果

# I don't know what you want to do with the results so
# I'll save them in a list...
results = [] 
for i in set_list:
    list_copy = list(set_list)
    list_copy.remove(i)
    r = i - functools.reduce(operator.or_, list_copy)
    results.append(r)

print(results)
# prints [set(), {9, 7}, {8, 10, 12, 45}, {11, 44, 23}]

【讨论】:

    【解决方案3】:

    这是使用标准库中的collections.Counter 重新实现NPE's answer

    from collections import Counter
    
    def mutual_difference(set_list):
        # create a multiset out of the union of all sets
        union = Counter()
        for s in set_list:
            union.update(s)
    
        new_set_list = []
        for s in set_list:
            # subtract s from the union and discard 0-count elements
            union.subtract(s)
            union += {}
    
            # take the difference
            new_set_list.append(s.difference(union))
    
            # add s to the union again
            union.update(s)
    
        return new_set_list
    

    例子:

    >>> mutual_difference([{1,2}, {2,3}, {1,4,5}])
    [set(), {3}, {4, 5}]
    

    【讨论】:

      【解决方案4】:
      [value - {item for subset in set_list[0:index] + set_list[index + 1:] for item in subset} for index, value in enumerate(set_list)]
      

      意思是:

      result = []
      for index, value in enumerate(set_list):
          union = {
              item
              for subset in set_list[0:index] + set_list[index + 1:]
              for item in subset
          }
          result.append(value - union)
      
      print(result)
      

      输出:

      [set(), {9, 7}, {8, 10, 12, 45}, {11, 44, 23}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-25
        • 1970-01-01
        • 2020-02-18
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        相关资源
        最近更新 更多