简体   繁体   English

在 Python 中合并集合内的集合

[英]Merging sets within sets in Python

I have a set (any collection type will do for all I care) like this.我有一个像这样的集合(任何集合类型都可以满足我的要求)。

myset = {{a},{b},{c},{d,e}, {d,f}}

I am now trying to check whether two elements {a} and {d} exist in myset and if so merge {a} with {d,e}.我现在正在尝试检查两个元素 {a} 和 {d} 是否存在于 myset 中,如果存在则将 {a} 与 {d,e} 合并。 If on the other hand I am checking {d} and {f}, then I will merge {d,e} and {d,f} to get {d,e,f}.另一方面,如果我正在检查 {d} 和 {f},那么我将合并 {d,e} 和 {d,f} 以获得 {d,e,f}。

I will be grateful if any one shows me the direction towards the solution.如果有人向我展示解决方案的方向,我将不胜感激。

This might not be the most pythonic approach but it should work,let me know if it doesn't and I have done it for lists,you could do it for sets using a similar approach 这可能不是最pythonic的方法,但它应该可以工作,请让我知道是否可以,并且我已经完成了对列表的操作,可以使用类似的方法对集合进行操作

def get_member(a,the_list):
    if a in the_list:
        return a
    else:
        for elem in the_list:
            if isinstance(elem,list):
                if a in elem:
                    return elem
    return None

def merge_member(a,b,the_list):
    get_member_a = get_member(a,the_list)
    get_member_b = get_member(b,the_list)
    if get_member_a and get_member_b:
        the_list.remove(get_member_a)
        the_list.remove(get_member_b)
        if isinstance(get_member_b,list):
            get_member_b.extend(get_member_a)
            the_list.append(get_member_b)
        elif isinstance(get_member_a,list):
            get_member_a.extend(get_member_b)
            the_list.append(get_member_a)
        else:
            the_list.append([get_member_b,get_member_a])
    return the_list

First of all: you cannot build a set of sets in python, as a set needs to contain hashable values while a set itself is not hashable .首先:您不能在 python 中构建一组集合,因为set需要包含可哈希值,而set本身不可哈希

But of course you could do the same with a list of of sets.但是当然你可以对集合列表做同样的事情。 The probably fastest and most pythonic solution should be like:可能最快和最 pythonic 的解决方案应该是这样的:

myset = [{1}, {2}, {3}, {4, 5}, {5, 6}]
merged_set = set().union(*myset)

(set.union cannot only be used with one parameter,but with any number of parameters ) (set.union 不能只和一个参数一起使用,可以和任意多个参数一起使用

I appears that we need to improve the merge_member block to handle such cases where we have [['a'],['b'],['c'],['d'],['e','f','g']], as the_list and we are looking for 'e' and 'f'. 我似乎需要改进merge_member块,以处理具有[['a'],['b'],['c'],['d'],['e','f' ,'g']],作为the_list,我们正在寻找'e'和'f'。

def merge_member(a,b,the_list):
    get_member_a = get_member(a, the_list)
    get_member_b = get_member(b, the_list)
    if get_member_a and get_member_b:
        if get_member_a.__eq__(get_member_b):
           the_list.remove(get_member_a)
           if isinstance(get_member_b, list):
              the_list.append(get_member_b)
        else:
           the_list.remove(get_member_a)
           the_list.remove(get_member_b)
           if isinstance(get_member_b, list):
              get_member_b.extend(get_member_a)
              the_list.append(get_member_b)
           elif isinstance(get_member_a, list):
              get_member_a.extend(get_member_b)
              the_list.append(get_member_a)
           else:
              the_list.append([get_member_b,get_member_a])
    return the_list

You can use this method to merge a set list: 您可以使用此方法合并设置列表:

from functools import reduce # for python3
myset = [{'a'}, {'b'}, {'c'}, {'d','e'}, {'d','f'}] 
reduce(lambda x, y: {*x, *y}, myset)

output: 输出:

{'a', 'b', 'c', 'd', 'e', 'f'}

Solution for the question: 问题的解决方案:

from functools import reduce # for python3
myset = [{'a'}, {'b'}, {'c'}, {'d','e'}, {'d','f'}]  # define your set list

def get_element_set(elem, myset):
    try:
        z = [x for x in myset if elem in x] 
        result = reduce(lambda x, y: {*x, *y}, z)
        return result
    except TypeError as e:
        print('Element "%s" is not exist in myset!' % elem)
        return {}

def merge_member(elem_list, myset):
    z = map(lambda elem: get_element_set(elem, myset), elem_list)
    result = reduce(lambda x, y: {*x, *y}, z)
    return result

Example: 例:

get_element_set('d', myset) # {'d', 'e', 'f'}
get_element_set('g', myset) # Element "g" is not exist in myset! {}
merge_member(['f', 'd'], myset) # {'d', 'e', 'f'}
merge_member(['a', 'd'], myset) # {'a', 'd', 'e', 'f'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM