Regarding sort()

Peter Otten __peter__ at web.de
Mon May 25 04:13:03 EDT 2009


Peter Otten wrote:

>>>> rows = data.splitlines()
>>>> rows.sort(key=lambda line: int(line.split()[5]))
>>>> rows.sort(key=lambda line: int(line.split()[2]))
>>>> print "\n".join(rows)

Of course you can also sort in a single step:

>>> rows = data.splitlines()
>>> def key(row):
...     columns = row.split()
...     return int(columns[2]), int(columns[5])
...
>>> rows.sort(key=key)
>>> print "\n".join(rows)

Peter




More information about the Python-list mailing list