This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
other:python:misc_by_jyp [2023/11/29 10:32] jypeter [Working with paths and filenames] Improved and added pathlib example |
other:python:misc_by_jyp [2024/04/19 12:02] (current) jypeter [Data representation] Corrected link to an old JYP tutorial |
||
---|---|---|---|
Line 5: | Line 5: | ||
</WRAP> | </WRAP> | ||
+ | ===== Extra tutorials ===== | ||
+ | Only **when you have already read all the content of this page several times**, and you are looking for new ideas | ||
+ | |||
+ | * [[https://medium.com/@yaduvanshineelam09/ultimate-python-cheat-sheet-practical-python-for-everyday-tasks-8a33abc0892f|Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks]] | ||
===== Reading/setting environments variables ===== | ===== Reading/setting environments variables ===== | ||
Line 313: | Line 317: | ||
['c', 'd', 'b', 'a']</code> | ['c', 'd', 'b', 'a']</code> | ||
+ | |||
+ | ===== Efficient looping with numpy, map, itertools and list comprehension ===== | ||
+ | |||
+ | <wrap hi>Big, nested, explicit ''for'' loops should be avoided at all cost</wrap>, in order to reduce a script execution time! | ||
+ | |||
+ | * **''numpy'' arrays** should be used when dealing with //numerical data// | ||
+ | * **Masked arrays** can be used to deal with //special cases// and remove tests from loops | ||
+ | |||
+ | * The built-in [[https://docs.python.org/3/library/functions.html?highlight=map#map|map]] function (and similar functions like [[https://docs.python.org/3/library/functions.html?highlight=zip#zip|zip]], [[https://docs.python.org/3/library/functions.html?highlight=filter#filter|filter]], ...) can be used to efficiently apply a function (possibly a //simple// [[https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions|lambda]] function) to all the elements of a list | ||
+ | * <code>>>> my_ints = [1, 2, 3] | ||
+ | |||
+ | >>> map(str, my_ints) | ||
+ | ['1', '2', '3'] | ||
+ | |||
+ | >>> map(lambda ii: str(10*ii + 5), my_ints) | ||
+ | ['15', '25', '35']</code> | ||
+ | |||
+ | * The [[https://docs.python.org/3/library/itertools.html|itertools]] module defines many more fancy iterators that can be used for efficient looping | ||
+ | * Example: replacing nested loops with [[https://docs.python.org/3/library/itertools.html#itertools.product|product]] | ||
+ | * <code>>>> it.product('AB', '01') | ||
+ | <itertools.product object at 0x2b35a7b5f100> | ||
+ | |||
+ | >>> list(it.product('AB', '01')) | ||
+ | [('A', '0'), ('A', '1'), ('B', '0'), ('B', '1')] | ||
+ | |||
+ | >>> for c1, c2 in it.product('AB', '01'): | ||
+ | ... print(c1 + c2) | ||
+ | ... | ||
+ | A0 | ||
+ | A1 | ||
+ | B0 | ||
+ | B1 | ||
+ | |||
+ | >>> for c1, c2 in it.product(['A', 'B'], ['0', '1']): | ||
+ | ... print(c1 + c2) | ||
+ | ... | ||
+ | A0 | ||
+ | A1 | ||
+ | B0 | ||
+ | B1 | ||
+ | |||
+ | >>> for c1, c2, c3 in it.product('AB', '01', '$!'): | ||
+ | ... print(c1 + c2 + c3, end=', ') | ||
+ | ... | ||
+ | A0$, A0!, A1$, A1!, B0$, B0!, B1$, B1!,</code> | ||
+ | |||
+ | * The [[https://docs.python.org/3/tutorial/datastructures.html?highlight=comprehension#list-comprehensions|list comprehension]] (aka //implicit loops//) can also be used to generate lists from lists | ||
+ | * Example: converting a list of integers to a list of strings\\ Note: in that case, you should rather use the ''map'' function detailed above | ||
+ | * <code>>>> my_ints = [1, 2, 3] | ||
+ | |||
+ | >>> [ str(ii) for ii in my_ints ] | ||
+ | ['1', '2', '3']</code> | ||
===== numpy related stuff ===== | ===== numpy related stuff ===== | ||
Line 492: | Line 548: | ||
A few notes for a future section or page about about //data representation// (bits and bytes) on disk and in memory, vs //data format// | A few notes for a future section or page about about //data representation// (bits and bytes) on disk and in memory, vs //data format// | ||
- | FIXME Add parts (pages 28 to 37) of this [[https://wiki.lsce.ipsl.fr/pmip3/doku.php/other:python:jyp_steps#part_2|old tutorial]] to this section | + | FIXME Add parts (pages 28 to 37) of this [[http://www.lsce.ipsl.fr/Phocea/file.php?class=page&file=5/pythonCDAT_jyp_2sur2_070306.pdf|old tutorial]] to this section |
==== Base notions ==== | ==== Base notions ==== |