Python: 'numpy.int32' object is not iterable

2

I'm constantly having this error and I do not know how to fix it.

This is the code:

import numpy as np
import itertools
import random

g_row=10
g_col=10

m_prob = np.ones((g_row, g_col), dtype=np.int)

s_acomulada = list(itertools.accumulate(itertools.chain.from_iterable(m_prob)))

papeletas = np.ones((g_row, g_col), dtype=np.int)

for i, fila in enumerate(s_acomulada):
  for j, n in enumerate(fila):
    papeletas[i][j] = tuple(range(inicio, inicio+n))
    inicio += n

print(papeletas)

And when you execute it, it returns this:

TypeError: 'numpy.int32' object is not iterable

I would also like m_prob, s_computed and ballots to be the three 10x10 matrices, as m_prob, if possible.

Greetings, thank you.

    
asked by Nexobeta28 YT 26.05.2018 в 02:26
source

1 answer

1

The error occurs in the second for nested because fila is an integer. This happens because s_acumulada is a one-dimensional list of integers since to calculate the accumulated sum it is flattened m_prob previously.

Note that NumPy has its own function for making the accumulated sum, numpy.cumsum When working with NumPy arrays, use the methods it provides whenever possible to preserve efficiency. To keep the dimensions simply apply the reshape method after carrying out the accumulated sum.

On the other hand, if at papeletas you are going to assign python tuples you can not initialize it with integer type, the NumPy arrays, unlike a python list, can not change type or mix types happily. You must initialize it with dtype object .

Finally, you need 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)
    
answered by 26.05.2018 / 09:42
source