[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
3 Réponses :
À la suite de votre code:
arr = list(arr) blocks = [arr[i:i+m] for i in range(0, len(arr), m)] assert len(blocks) == n
Si numpy est une option, vous pouvez simplement remodeler le tableau 1D:
print(arr.tolist()) # [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Vous pouvez le générer sous forme de tableau numpy
print(arr) # array([[ 1, 2, 3, 4], # [ 5, 6, 7, 8], # [ 9, 10, 11, 12]])
ou le convertir en liste imbriquée
s = input().split() n,m = int(s[0]),int(s[1]) arr = numpy.array(s[2:], dtype=int).reshape((n,m))
Essayez de cette manière:
[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Résultat:
s = input().split() s = list(map(int, s)) [s[2 + (i - 1) * s[1]: 2 + i * s[1]] for i in range(1, s[0] + 1)]