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 [2021/10/26 13:27] – Added sorting section jypeter | other:python:misc_by_jyp [2022/05/23 18:01] – [numpy related stuff] Changed the VIEW update warnng example jypeter | ||
---|---|---|---|
Line 39: | Line 39: | ||
True</ | True</ | ||
+ | ==== Playing with strings ==== | ||
+ | |||
+ | === Filenames, etc... === | ||
+ | |||
+ | Check [[other: | ||
+ | |||
+ | === Splitting strings === | ||
+ | |||
+ | It's easy to split a string with multiple blank delimiters, or a specific delimiter, but it can be harder to deal with sub-strings | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
==== Working with paths and filenames ==== | ==== Working with paths and filenames ==== | ||
Line 165: | Line 190: | ||
Check the [[https:// | Check the [[https:// | ||
+ | |||
+ | ==== Using sets ==== | ||
+ | |||
+ | [[https:// | ||
==== Printing a readable version of long lists or dictionaries ==== | ==== Printing a readable version of long lists or dictionaries ==== | ||
Line 202: | Line 231: | ||
==== Sorting ==== | ==== Sorting ==== | ||
- | < | + | * When dealing with **numerical values**, you should use the [[https:// |
+ | * [[https:// | ||
+ | * Example: sorting the keys and the values of a dictionary, and then using the '' | ||
+ | * If we provide a '' | ||
+ | * < | ||
+ | |||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | [-1, 0, 5, 10] | ||
+ | |||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | ==== numpy related stuff ==== | ||
+ | |||
+ | === Dealing with a variable number of indices === | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | ... | ||
+ | [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) | ||
+ | >>> | ||
+ | (10, 10) | ||
+ | |||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0., 0., 1., 0., 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., 0., 1., 0., 0., 0.]]) | ||
+ | >>> | ||
+ | (4, 10) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[-1., | ||
+ | [-1., -1.], | ||
+ | [-1., -1.], | ||
+ | [-1., -1.]]) | ||
+ | >>> | ||
+ | array([[ 1., 0., 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., 0., 1., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 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., 0., 1., 0.], | ||
+ | [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])</ | ||
+ | |||
+ | === Finding and counting unique values === | ||
+ | |||
+ | Use '' | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | array([1. , 2. , 1. , 2. , 2. , 1.5, 1. , 1.5, 2. , 1.5]) | ||
+ | |||
+ | >>> | ||
+ | array([1. , 1.5, 2. ]) | ||
+ | >>> | ||
+ | >>> | ||
+ | array([1. , 1.5, 2. ]) | ||
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ])</ | ||
+ | |||
+ | === Applying a ufunc over all the elements of an array === | ||
+ | |||
+ | There are all sorts of //ufuncs// (Universal Functions), and we will just use below '' | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | 15.5 | ||
+ | >>> | ||
+ | 15.5 | ||
+ | >>> | ||
+ | 15.5 | ||
+ | |||
+ | # Compute the sum of the elements of ' | ||
+ | # AND keep (accumulate) the intermediate results | ||
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | >>> | ||
+ | array([ 3, 6, 10]) | ||
+ | |||
+ | # The accumulated values can be used as indices to separate the different groups of sorted values! | ||
+ | >>> | ||
+ | array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ]) | ||
+ | >>> | ||
+ | array([1., 1., 1.]) | ||
+ | >>> | ||
+ | array([1.5, 1.5, 1.5]) | ||
+ | >>> | ||
+ | array([2., 2., 2., 2.]) | ||
+ | |||
+ | # Compute the sum of each equal-value group | ||
+ | >>> | ||
+ | (3.0, 4.5, 8.0)</ | ||
+ | |||
+ | === Applying a ufunc over specified sections of an array === | ||
+ | |||
+ | The [[https:// | ||
+ | |||
+ | < | ||
+ | # We need to add the beginning index (0), AND remove the last index | ||
+ | # (reduceat will automatically go to the end of the input array | ||
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | >>> | ||
+ | >>> | ||
+ | 10 | ||
+ | >>> | ||
+ | [0, 3, 6] | ||
+ | |||
+ | # Compute the sums over the selected intervals with just one call | ||
+ | >>> | ||
+ | array([3. , 4.5, 8. ])</ | ||
/* | /* |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter