[SciPy-user] What's the equivalent of list.pop in numpy?

2,828 views
Skip to first unread message

Armando Serrano Lombillo

unread,
Mar 25, 2008, 9:15:33 AM3/25/08
to SciPy Users List
Hello:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> print a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = a.pop(1)
>>> print a
[[1, 2, 3], [7, 8, 9]]
>>> print b
[4, 5, 6]
>>> from numpy import array
>>> a = array([[1,2,3],[4,5,6],[7,8,9]])
>>> print a
[[1 2 3]
[4 5 6]
[7 8 9]]
>>> b = a.pop(1)

Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
b = a.pop(1)
AttributeError: 'numpy.ndarray' object has no attribute 'pop'
>>>

So, what's the "right" way of doing this in numpy?

Armando.
_______________________________________________
SciPy-user mailing list
SciPy...@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

Alan G Isaac

unread,
Mar 25, 2008, 9:32:03 AM3/25/08
to SciPy Users List
A NumPy ndarray will not have a ``pop`` method largely
because it is of a fixed size.

It seems that you do not really want an ndarray.
Maybe a Python array would do.

Or maybe you are trying to do something that does
not really require a ``pop`` ...

Cheers,
Alan Isaac

Joris De Ridder

unread,
Mar 25, 2008, 11:11:03 AM3/25/08
to SciPy Users List

On 25 Mar 2008, at 14:15, Armando Serrano Lombillo wrote:

> Hello:
>
>>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>>> print a
> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>> b = a.pop(1)
>>>> print a
> [[1, 2, 3], [7, 8, 9]]
>>>> print b
> [4, 5, 6]
>>>> from numpy import array
>>>> a = array([[1,2,3],[4,5,6],[7,8,9]])
>>>> print a
> [[1 2 3]
> [4 5 6]
> [7 8 9]]
>>>> b = a.pop(1)
>
> Traceback (most recent call last):
> File "<pyshell#14>", line 1, in <module>
> b = a.pop(1)
> AttributeError: 'numpy.ndarray' object has no attribute 'pop'
>>>>
>
> So, what's the "right" way of doing this in numpy?

The closest thing I can come up with, is

In [1]: from numpy import *
In [2]: a = array([[1,2,3],[4,5,6],[7,8,9]])
In [3]: b = a[1]
In [4]: a = delete(a,[1], axis=0)

but this introduces quite some overhead, so, as Alan said, ask
yourself whether this is really what you need.

Joris


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

Reply all
Reply to author
Forward
0 new messages