简体   繁体   English

新列表基于 Python 中另一个列表的索引

[英]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 XA1

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同时迭代A1X 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.

相关问题 python 根据另一个列表中的索引从一个列表中删除元素 - python remove elements from one list based on indices from another list 如何根据 Python 中另一个列表的(子列表)索引对列表进行分区 - How to partition a list based on (sublist) indices of another list in Python 根据 python 中的索引,将列表列表中的值替换为另一个列表的值 - Substituting the values from list of lists by the values of another list based on indices in python 如何根据另一个列表中的索引从列表中删除子列表? - How to remove sublist from a list based on indices in another list? 如何根据另一个列表中的索引从列表中获取子列表? - How to get a SubList from a list based on indices in another List? 根据另一个列表中的索引汇总一个列表中的元素 - Summing up elements from one list based on indices in another list 有没有python方法根据提供的新索引重新排序列表? - Is there a python method to re-order a list based on the provided new indices? 基于python中另一个列表的最大值对数组的列索引进行分组 - Grouping column indices of an array based on max value of another list in python 如何根据索引值从另一个列表中提取子列表 - How to extract a sublist from another list based on value with indices 将列表解压缩到python中另一个列表的索引中 - Unpack a List in to Indices of another list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM