numpy find position in array

Solutions on MaxInterview for numpy find position in array by the best coders in the world

showing results for - "numpy find position in array"
Davide
18 Jan 2017
1import numpy as np
2
3heroes_1d = np.array(["Batman", "Spiderman", "Batman", "Superman"])
4print(np.where(heroes_1d == 'Batman'))
5# (array([0, 2]),)
6print(heroes_1d[0], heroes_1d[2])
7# Batman Batman
8
9
10heroes_2d = np.array([
11    ["Batman", "Spiderman", "Batman", "Superman"],
12    ["Catwoman", "Batgirl", "Iron Man", "Batman"]
13])
14print(np.where(heroes_2d == 'Batman'))
15# (array([0, 0, 1]), array([0, 2, 3]))
16print(heroes_2d[0][0], heroes_2d[0][2], heroes_2d[1][3])
17# Batman Batman Batman
18