Python usage notes - iterable stuff: Difference between revisions
m (→Iterators) |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{#addbodyclass:tag_tech}} | |||
{{#addbodyclass:tag_prog}} | |||
{{Pythonstuff}} | {{Pythonstuff}} | ||
Latest revision as of 12:12, 17 May 2024
Syntaxish: syntax and language · changes and py2/3 · decorators · importing, modules, packages · iterable stuff · concurrency · exceptions, warnings
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: