Sorting things into bins

Andrae Muys amuys at shortech.com.au
Thu Dec 13 21:47:25 EST 2001


"Sean 'Shaleh' Perry" <shalehperry at attbi.com> wrote in message news:<mailman.1008262758.25790.python-list at python.org>...
> def sorter(bins, item):
>         bin = item[-2:]
>         bins.setdefault(bin, []).append(item)
> 
> mylist = ['0102', '0302', '1242']
> mybins = {}
> map(lambda x: sorter(mybins, x), mylist)
> print mybins

... and just because I think List Comprehensions are really cool.....

>>> mylist = ['0101', '0102', '0103', '0201', '0202', '0203']
>>> [bins.setdefault(s[-2:], []).append(s) for bins in [{}] for s in mylist]
[None, None, None, None, None, None]
>>> print bins
{'02': ['0102', '0202'], '03': ['0103', '0203'], '01': ['0101', '0201']}

Andrae Muys



More information about the Python-list mailing list