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 revisionPrevious revision
Next revisionBoth sides next revision
other:python:misc_by_jyp [2023/11/29 11:32] – [Working with paths and filenames] Improved and added pathlib example jypeterother:python:misc_by_jyp [2023/12/08 16:36] – Added the efficient looping section jypeter
Line 313: Line 313:
 ['c', 'd', 'b', 'a']</code> ['c', 'd', 'b', 'a']</code>
  
 +
 +===== Efficient looping with numpy, map and itertools =====
 +
 +<wrap hi>Big, nested, explicit 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>
 ===== numpy related stuff ===== ===== numpy related stuff =====
  
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki