Python numpy 等价于 list.pop?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39945410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:57:55  来源:igfitidea点击:

numpy-equivalent of list.pop?

pythonarrayslistnumpystack

提问by Anton Alice

Is there a numpy method which is equivalent to the builtin popfor python lists?

是否有一个 numpy 方法相当于poppython 列表的内置方法?

Popping obviously doesn't work on numpy arrays, and I want to avoid a list conversion.

弹出显然不适用于 numpy 数组,我想避免列表转换。

回答by unutbu

There is no popmethod for NumPy arrays, but you could just use basic slicing (which would be efficient since it returns a view, not a copy):

popNumPy 数组没有方法,但您可以只使用基本切片(这将是有效的,因为它返回一个视图,而不是一个副本):

In [104]: y = np.arange(5); y
Out[105]: array([0, 1, 2, 3, 4])

In [106]: last, y = y[-1], y[:-1]

In [107]: last, y
Out[107]: (4, array([0, 1, 2, 3]))

If there were a popmethod it would return the lastvalue in yand modify y.

如果有一个pop方法,它会返回lasty并修改y

Above,

以上,

last, y = y[-1], y[:-1]

assigns the last value to the variable lastand modifies y.

将最后一个值赋给变量last并修改y.

回答by mozlingyu

Here is one example using numpy.delete():

这是一个使用示例numpy.delete()

import numpy as np
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(arr)
#  array([[ 1,  2,  3,  4],
#         [ 5,  6,  7,  8],
#         [ 9, 10, 11, 12]])
arr = np.delete(arr, 1, 0)
print(arr)
# array([[ 1,  2,  3,  4],
#        [ 9, 10, 11, 12]])

回答by dbouz

Pop doesn't exist for NumPy arrays, but you can use NumPy indexing in combination with array restructuring, for example hstack/vstack or numpy.delete(), to emulate popping.

NumPy 数组不存在 Pop,但您可以将 NumPy 索引与数组重组(例如 hstack/vstack 或 numpy.delete())结合使用来模拟弹出。

Here are some example functions I can think of (which apparently don't work when the index is -1, but you can fix this with a simple conditional):

以下是我能想到的一些示例函数(当索引为 -1 时显然不起作用,但您可以使用简单的条件来解决此问题):

def poprow(my_array,pr):
    """ row popping in numpy arrays
    Input: my_array - NumPy array, pr: row index to pop out
    Output: [new_array,popped_row] """
    i = pr
    pop = my_array[i]
    new_array = np.vstack((my_array[:i],my_array[i+1:]))
    return [new_array,pop]

def popcol(my_array,pc):
    """ column popping in numpy arrays
    Input: my_array: NumPy array, pc: column index to pop out
    Output: [new_array,popped_col] """
    i = pc
    pop = my_array[:,i]
    new_array = np.hstack((my_array[:,:i],my_array[:,i+1:]))
    return [new_array,pop]

This returns the array without the popped row/column, as well as the popped row/column separately:

这将返回没有弹出行/列的数组,以及分别弹出的行/列:

>>> A = np.array([[1,2,3],[4,5,6]])
>>> [A,poparow] = poprow(A,0)
>>> poparow
array([1, 2, 3])

>>> A = np.array([[1,2,3],[4,5,6]])
>>> [A,popacol] = popcol(A,2)
>>> popacol
array([3, 6])