Recommand · October 22, 2021 0

very simple code throws me : generator object is not subscriptable

Closed. This question needs debugging details. It is not currently accepting answers.


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 1 hour ago.

Improve this question

Here is my current code.

def get_unique_value(state):
    unique_value = 0
    for i in range(0, 3):
        for j in range(0, 3):
            unique_value += int((10**(i*3+j))*(state[i][j]))

which throws me a TypeError: ‘generator’ object is not subscriptable

here, state is a 3×3 numpy array, each having a value of 0, 1, or 2

Your code works:

def get_unique_value(state):
    unique_value = 0
    for i in range(0, 3):
        for j in range(0, 3):
            unique_value += int((10**(i*3+j))*(state[i][j]))
    return unique_value

state = np.random.randint(0, 4, (3,3))
print(get_unique_value(state))

# Output:
13233303

So I think state is not a numpy array, check:

>>> type(state)
numpy.ndarray

Reproducible error:

>>> state = (i for i in range(1))  # different from [i for i in range(1)]

>>> type(state)
generator

>>> get_unique_value(state)
...
TypeError: 'generator' object is not subscriptable

Are you sure you state is a numpy array?

There are no yield or return statements in your code, so the generator won’t return anything or be subscriptable.
Also, if you want to post a code snippet, use ` rather than ‘. It should be at the top left of your keyboard.