[英]New list based on indices from another list in Python
I have an array X
and a list A1
.我有一个数组X
和一个列表A1
。 I want to create a new list B1
such that it consists of values from X
corresponding to indices in A1
.我想创建一个新列表B1
,使其包含X
中对应于A1
中的索引的值。 For example, the code should pick values from X[0]
for indices in A1[0]
and so on...I present the current and expected outputs.例如,代码应该从X[0]
中为A1[0]
] 中的索引选择值,依此类推……我展示了当前和预期的输出。
import numpy as np
X= np.array([[417.551036, 0.0, 0.0, 353.856161, 0.0, 282.754301, 0.0, 0.0,
134.119055, 63.4573886, 208.344718, 1e-24],
[417.551036, 0.0, 332.821605, 294.983702, 0.0, 278.809292,
126.991664, 0.0, 136.02651, 83.1512525, 207.329562, 1e-24]])
A1=[[[3, 4, 6]], [[1, 3, 6]]]
for i in range(0,len(A1)):
for j in range(0,len(X)):
B1 = [[X[j][i] for i in indices] for indices in A1[i]]
print(B1)
The current output is当前output是
[[294.983702, 0.0, 126.991664]]
[[0.0, 294.983702, 126.991664]]
The expected output is预期的 output 是
[[353.856161, 0.0, 0.0]]
[[0.0, 294.983702, 126.991664]]
You can zip
the X
and A1
你可以zip
X
和A1
X = np.array([[417.551036, 0.0, 0.0, 353.856161, 0.0, 282.754301, 0.0, 0.0,
134.119055, 63.4573886, 208.344718, 1e-24],
[417.551036, 0.0, 332.821605, 294.983702, 0.0, 278.809292,
126.991664, 0.0, 136.02651, 83.1512525, 207.329562, 1e-24]])
A1 = [[[3, 4, 6]], [[1, 3, 6]]]
for x, a in zip(X, A1):
print(x[a])
or use np.take_along_axis
或者使用np.take_along_axis
A1 = np.array([[[3, 4, 6]], [[1, 3, 6]]])
shape = A1.shape
A1 = A1.reshape((shape[0], shape[2]))
print(np.take_along_axis(X, A1, 1))
Output Output
[[353.856161 0. 0. ]
[ 0. 294.983702 126.991664]]
for rowNumber, indices in enumerate(A1):
B1 = [X[rowNumber][index] for index in indices]
print(B1)
can be done with one list comprehension as well but little less readable IMO也可以通过一个列表理解来完成,但 IMO 的可读性稍差
list([X[rowNumber][index] for index in indices] for rowNumber, indices in enumerate(A1))
For the i
th element of A1
, you want to pick elements from the i
th row of X
, so iterate over A1
and X
simultaneously using zip
.对于A1
的第i
个元素,您想从X
的第i
行中选取元素,因此使用zip
同时迭代A1
和X
In the loop, ai
is a list containing a single list.在循环中, ai
是一个包含单个列表的列表。 This inner list contains the indices you want.这个内部列表包含你想要的索引。
result = []
for xi, ai in zip(X, A1):
indices = ai[0]
result.append(xi[indices].tolist())
Which gives the desired result
:这给出了期望的result
:
[[353.856161, 0.0, 0.0],
[0.0, 294.983702, 126.991664]]
Note that I converted xi[indices]
to a list, but if the result you want is a numpy array then you don't need to do that, and instead just:请注意,我将xi[indices]
转换为列表,但如果您想要的结果是 numpy 数组,那么您不需要这样做,而只需:
result = np.array([xi[ai[0]] for xi, ai in zip(X, A1)])
which gives a 2x3
result
array:它给出了一个2x3
result
数组:
array([[353.856161, 0. , 0. ],
[ 0. , 294.983702, 126.991664]])
This seems to work:这似乎有效:
B1 = []
for i, row in enumerate(A1):
values = []
for inner_row in row:
for index in inner_row:
values.append(X[i][index])
B1.append(values)
➜ arrays python main.py
[[353.856161, 0.0, 0.0], [0.0, 294.983702, 126.991664]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.