繁体   English   中英

如何在python中将列表拆分为没有重复元素的子集

[英]How to split a list into subsets with no repeating elements in python

我需要采用列表(最多n=31 )并返回n=3所有可能子集的代码,而没有任何两个元素在同一子集中重复两次(想想每次都与新人以 3 为一组的人):

list=[1,2,3,4,5,6,7,8,9]

并返回

[1,2,3][4,5,6][7,8,9]

[1,4,7][2,3,8][3,6,9]

[1,6,8][2,4,9][3,5,7]

但不是:

[1,5,7][2,4,8][3,6,9] 

因为 1 和 7 已经一起出现了(同样,3 和 9)。

我也想对n=2子集执行此操作。 谢谢!!

尝试这个:

from itertools import permutations

lst = list(range(1, 10))

n = 3
triplets = list(permutations(lst, n))
triplets = [set(x) for x in triplets]

def array_unique(seq):  
    checked = [] 
    for x in seq:
        if x not in checked: 
            checked.append(x) 
    return checked

triplets = array_unique(triplets)

result = []
m = n * 3
for x in triplets:
    for y in triplets:
        for z in triplets:
            if len(x.union(y.union(z))) == m:
                result += [[x, y, z]]

def groups(sets, i):
    result = [sets[i]]

    for x in sets:
        flag = True
        for y in result:
            for r in x:
                for p in y:
                    if len(r.intersection(p)) >= 2:
                        flag = False
                        break
                    else:
                        continue
                if flag == False:
                    break
        if flag == True:
            result.append(x)

    return result

for i in range(len(result)):
    print('%d:' % (i + 1))
    for x in groups(result, i):
        print(x)

输出n = 10: http//pastebin.com/Vm54HRq3

这是我想出的:

from itertools import permutations, combinations, ifilter, chain

people = [1,2,3,4,5,6,7,8,9]

#get all combinations of 3 sets of 3 people
combos_combos = combinations(combinations(people,3), 3)

#filter out sets that don't contain all 9 people
valid_sets = ifilter(lambda combo: 
                     len(set(chain.from_iterable(combo))) == 9,
                     combos_combos)

#a set of people that have already been paired
already_together = set()
for sets in valid_sets:
    #get all (sorted) combinations of pairings in this set
    pairings = list(chain.from_iterable(combinations(combo, 2) for combo in sets))
    pairings = set(map(tuple, map(sorted, pairings)))

    #if all of the pairings have never been paired before, we have a new one
    if len(pairings.intersection(already_together)) == 0:
        print sets
        already_together.update(pairings)

这打印:

~$ time python test_combos.py 
((1, 2, 3), (4, 5, 6), (7, 8, 9))
((1, 4, 7), (2, 5, 8), (3, 6, 9))
((1, 5, 9), (2, 6, 7), (3, 4, 8))
((1, 6, 8), (2, 4, 9), (3, 5, 7))

real        0m0.182s
user        0m0.164s
sys         0m0.012s

这是我尝试对您的问题进行相当普遍的解决方案。

from itertools import combinations

n = 3
l = range(1, 10)

def f(l, n, used, top):
    if len(l) == n:
        if all(set(x) not in used for x in combinations(l, 2)):
            yield [l]
    else:
        for group in combinations(l, n):
            if any(set(x) in used for x in combinations(group, 2)):
                continue
            for rest in f([i for i in l if i not in group], n, used, False):
                config = [list(group)] + rest
                if top:
                    # Running at top level, this is a valid
                    # configuration.  Update used list.
                    for c in config:
                        used.extend(set(x) for x in combinations(c, 2))
                yield config
                break

for i in f(l, n, [], True):
    print i

然而,对于高n值,它非常慢,对于n=31来说太慢。 我现在没时间尝试提高速度,但我可能会稍后再试。 欢迎提出建议!

我的妻子在尝试安排与九个人开会的分组会议时遇到了这个问题; 她不希望有成对的与会者重复。

我立即淘汰了 itertools 并被难住并来到了 StackOverflow。 但与此同时,我的非程序员妻子直观地解决了它。 关键的见解是创建井字棋网格:

1 2 3 
4 5 6
7 8 9

然后简单地将 3 组向下,3 组穿过,3 组对角线环绕,3 组对角线相反,环绕。

然后你可以在你的脑海中做到这一点。

- : 123,456,789 
| : 147,258,368 
\ : 159,267,348 
/ : 168,249,357 

我想下一个问题是你能在多大程度上采用这样的视觉方法? 它是否依赖于期望子集大小*子集数=总元素数的巧合?

暂无
暂无

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

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