Python usage notes - iterable stuff: Difference between revisions
m (→Iterators) |
m (→Iterators) |
||
Line 45: | Line 45: | ||
: there's more to it - read [https://peps.python.org/pep-3114/ PEP 3114] for details | : there's more to it - read [https://peps.python.org/pep-3114/ PEP 3114] for details | ||
--> | |||
===Enumerate=== | |||
<!-- | |||
Enumerate wraps an iterable, | |||
and additionally yields how many things it has yielded. This is useful for code like: | |||
for offset, item in enumerate(items): | |||
Notes: | |||
* an enumeration object hs no length, even if the iterable you gave it did | |||
: because that iterable may be exhausted, there is not necessarily any way to get it without | |||
--> | --> |
Latest revision as of 17:27, 3 March 2024
Syntaxish: syntax and language · changes and py2/3 · decorators · importing, modules, packages · iterable stuff · concurrency
IO: networking and web · filesystem Data: Numpy, scipy · pandas, dask · struct, buffer, array, bytes, memoryview · Python database notes Image, Visualization: PIL · Matplotlib, pylab · seaborn · bokeh · plotly
Stringy: strings, unicode, encodings · regexp · command line argument parsing · XML speed, memory, debugging, profiling · Python extensions · semi-sorted |
Iterators
Enumerate
Sorting
sorted() for a sorted copy
object.sort() for in-place
This is stable sorting, so you can do secondary sorting in multiple passes.
You can also do it in one pass, using key
key argument (py2, py3)
The key parameter should be a callable that fetches the value to sort on from an object.
This allows:
- normalization, e.g. case-insensitive sort via:
sorted(['foo','BAR'], key=lambda x: x.lower()) == ['BAR', 'foo']
- sorting by specific columns
sorted( [('b',2,),('a',1)], key=lambda l: l[1] ) == [('a', 1), ('b', 2)]
- sorting by multiple columns (or other aspects), when you make the key function return a tuple, for example:
- e.g. empty strings last, via:
sorted( ['b','','A'], key=lambda x: (x=='', x.lower())) == ['A', 'b', '']
cmp argument (removed in py3)
You can write an arbitrary comparison function, like
sorted( [[1,2], [2,1]], lambda a,b: cmp(b[1], a[1])) # second column descending
Almost all cmp functions I've seen can be easily rewritten with key (and reverse), possibly decorate-sort-undecorate. In this case:
sorted( [[1,2], [2,1]], key=lambda x: x[1], reverse=True)
As key tends to be faster, cmp is sort of redundant, and python3 removed cmp.
If you've got some cmp functions that are awkward to rewrite, you can use functools.cmp_to_key as a stopgap
See also: