other:python:misc_by_jyp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
other:python:misc_by_jyp [2022/05/23 17:36] – [numpy related stuff] Added the "variable number of indices" section jypeter | other:python:misc_by_jyp [2022/07/08 16:00] – [numpy related stuff] Added the arbitrary object array jypeter | ||
---|---|---|---|
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:// | ||
+ | |||
+ | It is also possible to store **arbitrary** Python objects in an array, rather than using nested lists or dictionaries! | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | array([[None, | ||
+ | | ||
+ | >>> | ||
+ | (2, 3) | ||
+ | >>> | ||
+ | None | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[None, | ||
+ | | ||
+ | None, None]], dtype=object)</ | ||
+ | | ||
=== Dealing with a variable number of indices === | === Dealing with a variable number of indices === | ||
Line 291: | Line 311: | ||
(4, 10) | (4, 10) | ||
- | >>> | + | >>> |
- | >>> | + | >>> |
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[-1., | ||
+ | [-1., -1.], | ||
+ | [-1., -1.], | ||
+ | [-1., -1.]]) | ||
>>> | >>> | ||
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.], | + | |
- | [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], | + | |
- | [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], | + | |
- | [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.], | + | |
[ 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.]])</ |
- | </ | + | |
=== Finding and counting unique values === | === Finding and counting unique values === |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter