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?

quarky
  • 333
  • 1
  • 2
  • 12
  • 3
    Why exactly do you think you need to switch back to a list? – jonrsharpe Jun 26 '15 at 09:46
  • It happens that I need the remove some elements from the list after I've calculated something with all elements. – quarky Jun 26 '15 at 09: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](http://stackoverflow.com/questions/10996140/how-to-remove-specific-elements-in-a-numpy-array) – tmdavison Jun 26 '15 at 09: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. – rth Jun 26 '15 at 10:08
  • Thanks, that's interesting. – quarky Jun 26 '15 at 10:15

1 Answers1

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]),)
fferri
  • 18,285
  • 5
  • 46
  • 95
  • Why did I think these attributes didn't exist for numpy arrays? No idea. Thank you. – quarky Jun 26 '15 at 10:15
  • @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)](https://wiki.python.org/moin/TimeComplexity) (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. – rth Jun 26 '15 at 13:02
  • 1
    @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. – rth Jun 26 '15 at 13:04
  • okay, thanks. I wasn't aware of this. I will think about how to improve my code considering this. – quarky Jun 28 '15 at 08:34