3

I know, there are plenty of threads about list vs. array but I've got a slightly different problem.

Using Python, I find myself converting between np.array and list quite often as I want to use attributes like

remove, append, extend, sort, index, … for lists

and on the other hand modify the content by things like

*, /, +, -, np.exp(), np.sqrt(), … which only works for arrays.

It must be pretty messy to switch between data types with list(array) and np.asarray(list), I assume. But I just can't think of a proper solution. I don't really want to write a loop every time I want to find and remove something from my array.

Any suggestions?

5
  • 3
    Why exactly do you think you need to switch back to a list? Commented Jun 26, 2015 at 9:46
  • It happens that I need the remove some elements from the list after I've calculated something with all elements. Commented Jun 26, 2015 at 9:58
  • 1
    numpy.delete will remove items from an array, if that's the only reason you are switching back to a list. See here Commented Jun 26, 2015 at 9:59
  • Also you can use numpy indexing to remove elements of an array. x = np.arange(5); mask = x>2; print( x[mask] ) will print only array([3, 4]) without needing to convert to a list. Commented Jun 26, 2015 at 10:08
  • Thanks, that's interesting. Commented Jun 26, 2015 at 10:15

1 Answer 1

3

A numpy array:

>>> A=np.array([1,4,9,2,7])

delete:

>>> A=np.delete(A, [2,3])
>>> A
array([1, 4, 7])

append (beware: it's O(n), unlike list.append which is O(1)):

>>> A=np.append(A, [5,0])
>>> A
array([1, 4, 7, 5, 0])

sort:

>>> np.sort(A)
array([0, 1, 4, 5, 7])

index:

>>> A
array([1, 4, 7, 5, 0])
>>> np.where(A==7)
(array([2]),)
Sign up to request clarification or add additional context in comments.

4 Comments

Why did I think these attributes didn't exist for numpy arrays? No idea. Thank you.
@quarky Note however that there is a fundamental difference in performance between list.append and np.append. While the former one has a complexity O(1) (i.e. executes in a fixed time), the later one is O(n) and the run time increases with the array size n, because very time the whole array will be reallocated. For instance, creating a list in a for loop with append is fine, while doing so with Numpy should be avoided at all costs. So those methods do exist in numpy, but you should understand the performance consequences before using them.
@quarky My point is that, remove, append, extend are rarely used in numpy, because often there is a more efficient way of achieving the same goal.
okay, thanks. I wasn't aware of this. I will think about how to improve my code considering this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.