如何使用numpy数组访问列表的条目而不使用for循环

时间:2022-01-15 21:21:01

Given a list L = [0,1,2,3,4,5,6,7,8,9]. What's the best way to access/extract elements where their indices are given by a numpy array? Let nArr=np.array([0,-1,2,6]).

给出列表L = [0,1,2,3,4,5,6,7,8,9]。访问/提取其索引由numpy数组给出的元素的最佳方法是什么?设nArr = np.array([0,-1,2,6])。

The resulting output should be another list P = [0,9,2,6].

结果输出应该是另一个列表P = [0,9,2,6]。

It is clear that when the elements are uniform in shape, we can simply cast it into another numpy array, but what if it isn't? For example, M = [np.random.rand(5,5), np.random.rand(1)].

很明显,当元素形状一致时,我们可以简单地将它转换成另一个numpy数组,但是如果它不是呢?例如,M = [np.random.rand(5,5),np.random.rand(1)]。

2 个解决方案

#1


2  

Stock Python has a convenience class, itemgetter:

Stock Python有一个便利类,itemgetter:

In [27]: from operator import itemgetter
In [28]: L = [0,1,2,3,4,5,6,7,8,9]
In [29]: nArr=np.array([0,-1,2,6])
In [30]: itemgetter(*nArr)
Out[30]: operator.itemgetter(0, -1, 2, 6)
In [31]: itemgetter(*nArr)(L)
Out[31]: (0, 9, 2, 6)

Internally it does something equivalent to the list comprehension:

在内部,它做了与列表理解相当的事情:

In [33]: [L[x] for x in nArr]
Out[33]: [0, 9, 2, 6]

So it isn't a fast operation like the array indexing (look at its code). It may be most useful as a way of doing sort or other operations where you'd like to define a key function that fetches multiple values.

所以它不像数组索引那样快速运行(看看它的代码)。作为一种排序或其他操作的方式,它可能是最有用的,您可以在其中定义获取多个值的键函数。

https://*.com/a/47585659/901925

Make a random nested list:

创建一个随机嵌套列表:

In [54]: arr = np.random.randint(0,10,(4,4))
In [55]: L = arr.tolist()
In [56]: L
Out[56]: [[9, 5, 8, 4], [1, 5, 5, 8], [8, 0, 5, 8], [1, 4, 5, 1]]

lexical sort by 'column':

“列”的词汇排序:

In [57]: sorted(L)
Out[57]: [[1, 4, 5, 1], [1, 5, 5, 8], [8, 0, 5, 8], [9, 5, 8, 4]]

lexical sort by 'columns' 2 and 1 (in that order):

词汇排序由'列'2和1(按此顺序):

In [59]: sorted(L, key=itemgetter(2,1))
Out[59]: [[8, 0, 5, 8], [1, 4, 5, 1], [1, 5, 5, 8], [9, 5, 8, 4]]

#2


1  

To summarize the comments: lists do not support indexing by an array, like L[nArr] where nArr is an array of indexes. One normally uses list comprehension, [L[i] for i in nArr]. But if you want to, you can cast the list to a NumPy array of objects, which can then be indexed and sliced as any other NumPy array:

总结注释:列表不支持数组索引,如L [nArr],其中nArr是索引数组。一个人通常使用列表理解,[L [i] for i in nArr]。但是如果你愿意,你可以将列表转换为NumPy对象数组,然后可以像任何其他NumPy数组一样对其进行索引和切片:

np.array(L, dtype=object)[nArr]

If you want a list returned, you can do:

如果要返回列表,可以执行以下操作:

np.array(L, dtype=object)[nArr].tolist()

But that's not nearly as pythonic as list comprehension, requires more memory, and very likely doesn't improve the speed.

但这并不像列表理解那样pythonic,需要更多的内存,很可能不会提高速度。

#1


2  

Stock Python has a convenience class, itemgetter:

Stock Python有一个便利类,itemgetter:

In [27]: from operator import itemgetter
In [28]: L = [0,1,2,3,4,5,6,7,8,9]
In [29]: nArr=np.array([0,-1,2,6])
In [30]: itemgetter(*nArr)
Out[30]: operator.itemgetter(0, -1, 2, 6)
In [31]: itemgetter(*nArr)(L)
Out[31]: (0, 9, 2, 6)

Internally it does something equivalent to the list comprehension:

在内部,它做了与列表理解相当的事情:

In [33]: [L[x] for x in nArr]
Out[33]: [0, 9, 2, 6]

So it isn't a fast operation like the array indexing (look at its code). It may be most useful as a way of doing sort or other operations where you'd like to define a key function that fetches multiple values.

所以它不像数组索引那样快速运行(看看它的代码)。作为一种排序或其他操作的方式,它可能是最有用的,您可以在其中定义获取多个值的键函数。

https://*.com/a/47585659/901925

Make a random nested list:

创建一个随机嵌套列表:

In [54]: arr = np.random.randint(0,10,(4,4))
In [55]: L = arr.tolist()
In [56]: L
Out[56]: [[9, 5, 8, 4], [1, 5, 5, 8], [8, 0, 5, 8], [1, 4, 5, 1]]

lexical sort by 'column':

“列”的词汇排序:

In [57]: sorted(L)
Out[57]: [[1, 4, 5, 1], [1, 5, 5, 8], [8, 0, 5, 8], [9, 5, 8, 4]]

lexical sort by 'columns' 2 and 1 (in that order):

词汇排序由'列'2和1(按此顺序):

In [59]: sorted(L, key=itemgetter(2,1))
Out[59]: [[8, 0, 5, 8], [1, 4, 5, 1], [1, 5, 5, 8], [9, 5, 8, 4]]

#2


1  

To summarize the comments: lists do not support indexing by an array, like L[nArr] where nArr is an array of indexes. One normally uses list comprehension, [L[i] for i in nArr]. But if you want to, you can cast the list to a NumPy array of objects, which can then be indexed and sliced as any other NumPy array:

总结注释:列表不支持数组索引,如L [nArr],其中nArr是索引数组。一个人通常使用列表理解,[L [i] for i in nArr]。但是如果你愿意,你可以将列表转换为NumPy对象数组,然后可以像任何其他NumPy数组一样对其进行索引和切片:

np.array(L, dtype=object)[nArr]

If you want a list returned, you can do:

如果要返回列表,可以执行以下操作:

np.array(L, dtype=object)[nArr].tolist()

But that's not nearly as pythonic as list comprehension, requires more memory, and very likely doesn't improve the speed.

但这并不像列表理解那样pythonic,需要更多的内存,很可能不会提高速度。