我们正在努力解决以下问题:
例如,以下列表应按相同顺序分为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]]
等
我该如何实现?
答案 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]);
}
}