在Python中合并集合内的集合

时间:2014-11-25 07:30:36

标签: python list merge set

我有一套(任何集合类型都适用于所有我关心的),就像这样。

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

我现在正在尝试检查myset中是否存在两个元素{a}和{d},如果是,则将{a}与{d,e}合并。另一方面,如果我正在检查{d}和{f},那么我将合并{d,e}和{d,f}来获得{d,e,f}。

如果有人向我展示解决方案的方向,我将不胜感激。

3 个答案:

答案 0 :(得分:2)

这可能不是最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

答案 1 :(得分:0)

我似乎需要改进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

答案 2 :(得分:0)

您可以使用此方法合并集合列表:

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

输出:

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

问题的解决方案:

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

示例:

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'}