Locating list elements in an array in Python
Question:
I have two arrays A
and B
. I have a list indices
.
I want to locate each element of indices
in A
and print the corresponding values from B
. I present the current and expected outputs.
import numpy as np
A=np.array([[ 0, 4],
[ 0, 5],
[1,6]])
B=np.array([[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]])
indices= [[0,4],[1,6]]
for i in range(0,len(indices)):
A=indices[i]
print(A)
The current output is:
[0, 4]
[1, 6]
The expected output is:
[[0,4],[1,6]]
[[9.16435586e-05],[1.781909182e-5]]
Answers:
To generate a list based on index match I would do it like that, you can also modify this based on your use case
new_list = []
for i, elem in enumerate(A):
if list(elem) in indices:
new_list.append(B[i])
common_index=[x for x,y in enumerate(A) if list(y) in indices]
#[0, 2]
lst=[]
for t in common_index:
lst.append(list(B[t]))
#output
[[9.16435586e-05], [1.781909182e-05]]
Numpy is good at processing arrays of numbers in a vectorized way. Here, you would better use plain Python lists:
A = [[ 0, 4],
[ 0, 5],
[1,6]]
B = [[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]]
indices= [[0,4],[1,6]]
print([A[i] for i,v in enumerate(A) if v in indices])
print([B[i] for i,v in enumerate(A) if v in indices])
gives as expected:
[[0, 4], [1, 6]]
[[9.16435586e-05], [1.781909182e-05]]
You can use numpy’s where method to achieve this:
import numpy as np
A=np.array([[ 0, 4],
[ 0, 5],
[1,6]])
B=np.array([[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]])
indices= [[0,4],[1,6]]
for i in range(0,len(indices)):
index = np.where(A == i)
print(index)
print(A[index])
print(B[index])
I have two arrays A
and B
. I have a list indices
.
I want to locate each element of indices
in A
and print the corresponding values from B
. I present the current and expected outputs.
import numpy as np
A=np.array([[ 0, 4],
[ 0, 5],
[1,6]])
B=np.array([[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]])
indices= [[0,4],[1,6]]
for i in range(0,len(indices)):
A=indices[i]
print(A)
The current output is:
[0, 4]
[1, 6]
The expected output is:
[[0,4],[1,6]]
[[9.16435586e-05],[1.781909182e-5]]
To generate a list based on index match I would do it like that, you can also modify this based on your use case
new_list = []
for i, elem in enumerate(A):
if list(elem) in indices:
new_list.append(B[i])
common_index=[x for x,y in enumerate(A) if list(y) in indices]
#[0, 2]
lst=[]
for t in common_index:
lst.append(list(B[t]))
#output
[[9.16435586e-05], [1.781909182e-05]]
Numpy is good at processing arrays of numbers in a vectorized way. Here, you would better use plain Python lists:
A = [[ 0, 4],
[ 0, 5],
[1,6]]
B = [[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]]
indices= [[0,4],[1,6]]
print([A[i] for i,v in enumerate(A) if v in indices])
print([B[i] for i,v in enumerate(A) if v in indices])
gives as expected:
[[0, 4], [1, 6]]
[[9.16435586e-05], [1.781909182e-05]]
You can use numpy’s where method to achieve this:
import numpy as np
A=np.array([[ 0, 4],
[ 0, 5],
[1,6]])
B=np.array([[9.16435586e-05],
[1.84193464e-14],
[1.781909182e-5]])
indices= [[0,4],[1,6]]
for i in range(0,len(indices)):
index = np.where(A == i)
print(index)
print(A[index])
print(B[index])