Number of elements in ndarray vector

Asked

Viewed 37 times

-1

I have the following function to count how many "player" appear in the array estado[i], which is a line of a matrix:

for i in range (0, len(estado)):
    if (estado[i].count(jogador) == len(estado)):
        return True

But "state" is the type ndarray, how could count the amount of occurrences of "player" in estado[i] her being a ndarray?

1 answer

-1

I see the simplest way to filter a ndarray using the method where. From there, a tuple is returned containing an array of the items found in the first index, just check the property shape of the same:

for i in range (0, len(estado)):
    # supondo que estado[i] tenha apenas uma dimensão e cada item corresponda a um jogador 
    # e acessando o primeiro item da tupla de retorno das chamadas a "where" e "shape"
    if (np.where(estado[i] == jogador)[0].shape[0] == len(estado)):
        return True

And as an example:

import numpy as np

x = np.array([['jogador 1', 'jogador 2'], ['jogador 2', 'jogador 3']])

for i in range(0, len(x)):
  print(np.where(x[i] == 'jogador 1')[0].shape[0])

The result of the example would be:

1
0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.