User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2022/05/23 15:36]
jypeter [numpy related stuff] Added the "variable number of indices" section
other:python:misc_by_jyp [2022/07/08 14:00]
jypeter [numpy related stuff] Added the arbitrary object array
Line 248: Line 248:
 ==== numpy related stuff ==== ==== numpy related stuff ====
  
 +=== Using a numpy array to store arbitrary objects ===
 +
 +The numpy arrays are usually used to store [[https://​numpy.org/​doc/​stable/​reference/​arrays.scalars.html|scalars]] of the same type (see also the [[https://​numpy.org/​doc/​stable/​reference/​arrays.dtypes.html|Data type objects (dtype)]]), very often numerical values.
 +
 +It is also possible to store **arbitrary** Python objects in an array, rather than using nested lists or dictionaries!
 +
 +<​code>>>>​ some_array = np.empty((2,​ 3), dtype=object)
 +>>>​ some_array
 +array([[None,​ None, None],
 +       ​[None,​ None, None]], dtype=object)
 +>>>​ some_array.shape
 +(2, 3)
 +>>>​ print(some_array[-1,​ -1])
 +None
 +>>>​ some_array[-1,​ 0] = filled_contour # e.g. save an existing cartopy filled contour object
 +>>>​ some_array
 +array([[None,​ None, None],
 +       ​[<​cartopy.mpl.contour.GeoContourSet object at 0x2ab679e8bf10>,​
 +        None, None]], dtype=object)</​code>​
 +        ​
 === Dealing with a variable number of indices === === Dealing with a variable number of indices ===
  
Line 291: Line 311:
 (4, 10) (4, 10)
  
->>>​ # WARNING! ​WARNINGA slice is a VIEW and NOT A COPY +>>>​ # WARNING! ​DANGERRRRNEVER forget that a VIEW is NOT A COPY 
->>>​ i10[my_fancy_slices] = -1+>>> ​# and that you can change the content of the original array by mistake 
 +>>>​ my_view = i10[my_slices] 
 +>>>​ my_view[:, :] = -1 
 +>>>​ my_view 
 +array([[-1.,​ -1.], 
 +       [-1., -1.], 
 +       [-1., -1.], 
 +       [-1., -1.]])
 >>>​ i10 >>>​ i10
 array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.], array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
-       [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], +       ​[ ​0.,  0.,  0.,  1., -1., -1.,  0.,  0.,  0.,  0.], 
-       [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], +       ​[ ​0.,  0.,  0.,  0., -1., -1.,  0.,  0.,  0.,  0.], 
-       [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], +       ​[ ​0.,  0.,  0.,  0., -1., -1.,  0.,  0.,  0.,  0.], 
-       [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],+       ​[ ​0.,  0.,  0.,  0., -1., -1.,  1.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
-       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]]) +       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]])</​code>​
-</​code>​+
  
 === Finding and counting unique values === === Finding and counting unique values ===
other/python/misc_by_jyp.txt · Last modified: 2023/12/08 15:51 by jypeter