R
The error occurs in the second for annoying because fila It's an integer. This happens because s_acumulada is a one-dimensional list of integers since to calculate the accumulated sum is flattened m_prob previously.Note that NumPy has its own function to perform the accumulated sum, https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html . When you work with NumPy arrays, use the methods it provides whenever you can to preserve efficiency. To retain dimensions simply apply the method reshape after carrying out the accumulated sum.On the other hand, if papeletas you're going to assign python tuplas you can't initialize it with whole type, NumPy arrays, unlike a python list, can't change You must initialize it with dtype object.Finally, you have to declare inicio before for.import numpy as np
g_row=200
g_col=200
m_prob = np.ones((g_row, g_col), dtype=np.int)
papeletas = np.empty((g_row, g_col), dtype=object)
s_acumulada = np.cumsum(m_prob, axis=None, dtype=np.int).reshape(m_prob.shape)
inicio=1
for i, fila in enumerate(s_acumulada):
for j, n in enumerate(fila):
papeletas[i][j] = tuple(range(inicio, inicio+n))
inicio += n
>>> m_prob
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
>>> m_prob.shape
(10, 10)
>>> s_acumulada
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
[ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
[ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
[ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
[ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
[ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
[ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
[ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]], dtype=int32)
>>> s_acumulada.shape
(10, 10)
>>> papeletas
array([[(1,), (2, 3), (4, 5, 6), (7, 8, 9, 10), (11, 12, 13, 14, 15),
(16, 17, 18, 19, 20, 21), (22, 23, 24, 25, 26, 27, 28),
(29, 30, 31, 32, 33, 34, 35, 36),
(37, 38, 39, 40, 41, 42, 43, 44, 45),
(46, 47, 48, 49, 50, 51, 52, 53, 54, 55)],
[(56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66),
...]...], dtype=object)
>>> papeletas.shape
(10, 10)