Sorting things into bins

Jason Orendorff jason at jorendorff.com
Wed Dec 12 20:00:28 EST 2001


> Is there a quick and simple way to sort a set of values (strings in this 
> case) into a set of bins, based on a subset of the characters in the 
> string?

mylist = ['0101', '0102', '0103', '0201', '0202', '0203']
mybins = {}
for s in mylist:
    bin = s[-2:]
    mybins.setdefault(bin, []).append(s)
print mybins

-- 
Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list