numpy Python程序将一个组划分为子组,但有例外

iecba09b  于 2023-05-07  发布在  Python
关注(0)|答案(2)|浏览(167)

我想写一个python代码,根据特定的标准将一个由几行组成的组划分为子组,其效果是每行可以加入包含某些行的组,但不允许加入包含其他行的组。例如:

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

输出:

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

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

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

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

更多说明:对于第一行[[2,3,5,6,8],[1,4,7,9],0],0是该行的编号,[2,3,5,6,8]组包含第2,3,5,6和8行,行号0不允许加入,但可以加入包含大多数这些行的组[1,4,7,9]。
另一个例子如下:

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

输出:

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

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

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

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

致上,

kknvjkwl

kknvjkwl1#

我的方法使用itertools来迭代可能的组合

import itertools

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

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

def getGroups(groups):
    used = []
    c = 3
    group_c = 0
    print()
    while len(used) < 10:
        for x in range(0,10):
            for combo in itertools.combinations(groups[x][1], c):
                result = []
                for group in groups:
                        if group[2] not in used and group[2] in combo and x not in group[0]:
                            if (all(elem not in group[0] for elem in combo)):
                                result.append(group)
                
                if len(result) == c:
                    group_c += 1
                    print("GROUP", group_c)
                    
                    if x not in used:
                        print(groups[x])
                    for treffer in result:
                        print(treffer)
                        used.append(treffer[2])
                    used.append(x)
        c = c-1

getGroups(Group_A)

getGroups(Group_B)

输出看起来和预期的一样:

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

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

jm81lzqq2#

下面的程序和代码可能有助于解决问题,但代码仍然有问题。
删除中间的列,并将行号与其余的列连接起来。如示例一所示:

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

然后将其排序为:

group = group[np.lexsort([group[:, i] for i in (range(0, group.shape[1] - 1))])][::-1]

结果:

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

分组代码如下:

def create_grouping(Group_A):
   temp1 = []  
   temp2 = []
   result = []
   size = len(group[0])
   for index in range(len(group) - 1):
        if group[index][size - 1] in group[index + 1] or len(set.intersection(set(group[index]), set(temp2))) > 0:
             temp1.append(group[index])
             result.append(temp1)
             temp1 = []
             temp2 = []
        else:
             temp2.append(group[index][size - 1])           
             temp1.append(group[index])

   print(" Result:", result)
   return

运行时:create_grouping(Group_A)我得到输出:

Result: [[array([3, 4, 7, 8, 9, 1]), array([0, 4, 7, 8, 9, 6]), array([0, 3, 7, 8, 9, 2]), array([1, 5, 6, 8, 9, 4])], [array([0, 2, 4, 7, 9, 8]), array([0, 2, 4, 7, 9, 5]), array([0, 1, 4, 7, 9, 3])]]

如图所示,最后一个组未创建,组项不准确。代码中缺少了一些东西。

相关问题