python - numpy-equivalent of list.pop? - Stack Overflow
Does pop(i) have a Time Complexity of O(n) or O(k)?
list.pop and list.pop()
AttributeError: 'numpy.ndarray' object has no attribute 'pop'
Videos
There is no pop method for NumPy arrays, but you could just use basic slicing (which would be efficient since it returns a view, not a copy):
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 pop method it would return the last value in y and modify y.
Above,
last, y = y[-1], y[:-1]
assigns the last value to the variable last and modifies y.
Here is one example using 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]])
I'm reading a book on data structures and algorithms in python and the say pop(i) is O(n) but on Python's website it states that pop intermediate is O(k)? See here: https://wiki.python.org/moin/TimeComplexity
To me it makes sense that it would be O(k) since you only have to shift the objects to the right of the index that you popped but please correct me if I'm misunderstanding something.
This feels like one of those python weird things. I am interested in explanations.
If I have a list=[1,2,3,4] and I do list.pop() the result is list=[1,2,3].
Perfect, just what I wanted.
However, if I am not careful and instead do list.pop--note there are no parentheses this time--I get no syntax error or warning and nothing happens, leading me to a strange debug session.
In the repl, if I do l.pop it just identifies it as a built-in method of list object at 0xwhatever. That's useful, but why is there not at least a runtime warning when I make this mistake in my code?