Retrieve array elements from a list of index

Nakeuh

I have an array my_array containing some tuple data.

I have another array my_array_values of same length containing some integer values.

For each unique value of my_array_values, I want to retrieve a list of values from my_array, that are in the same index as this value in my_array_values. Here is my code and the expected behavior :

my_array = np.array([('AA','11'),('BB','22'),('CC','33'),('DD','44'),('EE','55'),('FF','66')])
my_array_values = np.array([1,2,3,1,3,2])
my_array_values_unique = np.array([1,2,3])

for v in my_array_values_unique:
    print(np.take(my_array, np.where(my_array_values == v)))

Expected behavior :

[('AA', '11'), ('DD', '44')]
[('BB', '22'), ('FF', '66')]
[('CC', '33'), ('EE', '55')]

But actually, my code gives me the following output:

[['AA' '22']]
[['11' '33']]
[['BB' 'CC']]

Can someone explain to me how I can get the correct output?

Tomerikoo

You don't need to use take or where at all. Equality check on an array returns a boolean array which is a valid indexing array:

for v in my_array_values_unique:
    print(my_array[my_array_values == v])

And this prints:

[['AA' '11']
 ['DD' '44']]
[['BB' '22']
 ['FF' '66']]
[['CC' '33']
 ['EE' '55']]

If numpy is not specificly required, this can be easily done using lists as well:

lst = [('AA', '11'), ('BB', '22'), ('CC', '33'), ('DD', '44'), ('EE', '55'), ('FF', '66')]
idxs = [1, 2, 3, 1, 3, 2]

for v in set(idxs):
    print([tup for idx, tup in zip(idxs, lst) if idx == v])

Gives:

[('AA', '11'), ('DD', '44')]
[('BB', '22'), ('FF', '66')]
[('CC', '33'), ('EE', '55')]

Another, more efficient, way would be to use defaultdict in order to loop the list once, instead of once for every unique value:

import collections

mapping = collections.defaultdict(list)
for tup, idx in zip(lst, idxs):
    mapping[idx].append(tup)

for lst in mapping.values():
    print(lst)
  • gives the same result as before

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Summing Array Elements from Specific Index Range

How to retrieve array of elements from array of structure in golang?

Accessing NumPy array elements not in a given index list

Remove multiple elements from a list of index with Python

Retrieve an evenly distributed number of elements from an array

Naming elements of a nested list from their index in R

Retrieve index of JSON array from

Remove multiple elements from array based on index

Delete elements from python array with given index of elements as list

Advanced slicing: Given a list of index, pick different elements from a numpy array

retrieve elements from two-dimensional array

Extract same index elements from a list

Finding the index of elements in an array/list based on another list or array

Retrieve array elements from document in Mongoose

How to delete elements from a List synchronously by Index?

How to add and retrieve from array list?

Remove elements from array above an index AngularJS

Replacing elements in an array from index array

Array of index values for unique elements in list

How to remove unknown index elements from an array

index of N highest elements from a list of numpy array

Using a for-loop to retrieve elements from an Array

How to multiply elements of an array by elements from another array with the same index?

How to retrieve elements from an array

Remove elements from a List at a specific index

Get elements from list in python by ¿index?

how to retrieve array index from collection?

How to retrieve elements from an array without slicing?

Swift Array, how to retrieve all elements in a nested enum from an array