Python: list vs. np.array: jump to using certain attributes

I know there are many threads about list versus array, but I have a slightly different problem.

Using Python, I convert between np.array and a list quite often, as I want to use attributes like

remove, add, expand, sort, index, ... for lists

and on the other hand change the content with things like

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

It should be pretty messy to switch between combo (array) and np.asarray (list) data types, I suppose. But I just can't think of a correct solution. I don't want to write a loop every time I want to find and delete something from my array.

Any suggestions?

+3


source to share


1 answer


Numpy array:

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

      

delete:

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

      

append (be careful: 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]),)

      

+3


source







All Articles