获取排序列表的所有排序子列表

时间:2018-12-18 09:25:59

标签: python list sorting

我们正在努力解决以下问题:
例如,以下列表应按相同顺序分为m列表。

[9,7,6,5,2,10,8,4,3,1]m=3 最终结果应该是:

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

我该如何实现?

2 个答案:

答案 0 :(得分:1)

from itertools import combinations, permutations


perm=[]
index = [9,7,6,5,2,10,8,4,3,1]
perm.append(index)
M = 3
slicer = [x for x in combinations(range(1, len(index)), M - 1)]
slicer = [(0,) + x + (len(index),) for x in slicer]

result = [tuple(p[s[i]:s[i + 1]] for i in range(len(s) - 1)) for s in slicer for p in perm]

答案 1 :(得分:0)

解决方案:

n =列表长度

k = m = 3

        int[,] mas = new int[9,9];
        Random rand = new Random();
        for (int i = 0; i <mas.Length; i++)
        {
            mas[i] = rand.Next(0,9);
             Console.WriteLine(mas[i]);
            for (int k = 0; k < mas.Length; k++)
            {
                mas[k] = rand.Next(0,9);  
              Console.WriteLine(mas[k]);
            }

        }