Python usage notes - iterable stuff

From Helpful
Jump to navigation Jump to search
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


Tasky: Concurrency (threads, processes, more) · joblib · pty and pexpect

Stringy: strings, unicode, encodings · regexp · command line argument parsing · XML

date and time


Notebooks

speed, memory, debugging, profiling · Python extensions · semi-sorted


Iterators

Enumerate

Sorting

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.

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

Iteratable-related tools

See also: