in Array of Arrays effizient den index ermitteln

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Erhy
User
Beiträge: 64
Registriert: Mittwoch 2. Januar 2019, 21:09

Hallo!

wie kann ich den Index eines eingebetteten arrays ermitteln?

z.B.

Code: Alles auswählen

bigArray = np.array([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
thisArray = np.array([3, 4, 5])

np.asarray(np.where(bigArray == thisArray))
ergibt

Code: Alles auswählen

array([[1, 1, 1],
       [0, 1, 2]], dtype=int64)
anstatt

Code: Alles auswählen

array([[1]])
Danke für Eure Antwort
Erhy
Benutzeravatar
__blackjack__
User
Beiträge: 14336
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

Code: Alles auswählen

In [502]: rows                                                                  
Out[502]: 
array([[1, 2, 3],
       [3, 4, 5],
       [5, 6, 7]])

In [503]: needle                                                                
Out[503]: array([3, 4, 5])

In [504]: rows == needle                                                        
Out[504]: 
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]])

In [505]: (rows == needle).all(axis=1)                                          
Out[505]: array([False,  True, False])

In [506]: (rows == needle).all(axis=1).nonzero()                                
Out[506]: (array([1]),)
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Erhy
User
Beiträge: 64
Registriert: Mittwoch 2. Januar 2019, 21:09

danke für die schnelle Antwort.

die letzte Zeile habe ich noch modifiziert und hinzugefügt:

Code: Alles auswählen

d = (rows == needle).all(axis=1).nonzero()
arrInDuple = d[0]
i = arrInDuple[0]  # index of searchrd array in big array
Antworten