This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
other:python:misc_by_jyp [2023/12/08 15:36] jypeter Added the efficient looping section |
other:python:misc_by_jyp [2023/12/08 15:51] (current) jypeter [Efficient looping with numpy, map and itertools] Added list comprehension |
||
---|---|---|---|
Line 314: | Line 314: | ||
- | ===== Efficient looping with numpy, map and itertools ===== | + | ===== Efficient looping with numpy, map, itertools and list comprehension ===== |
- | <wrap hi>Big, nested, explicit loops should be avoided at all cost</wrap>, in order to reduce a script execution time! | + | <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// | * **''numpy'' arrays** should be used when dealing with //numerical data// | ||
Line 358: | Line 358: | ||
... | ... | ||
A0$, A0!, A1$, A1!, B0$, B0!, B1$, B1!,</code> | 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 ===== | ||