我想写一个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]]
致上,
2条答案
按热度按时间kknvjkwl1#
我的方法使用itertools来迭代可能的组合
输出看起来和预期的一样:
jm81lzqq2#
下面的程序和代码可能有助于解决问题,但代码仍然有问题。
删除中间的列,并将行号与其余的列连接起来。如示例一所示:
然后将其排序为:
结果:
分组代码如下:
运行时:create_grouping(Group_A)我得到输出:
如图所示,最后一个组未创建,组项不准确。代码中缺少了一些东西。