Sorting things into bins
Sue Giller
sag at hydrosphere.com
Thu Dec 13 11:50:17 EST 2001
Jason,
While I was hoping to not have to iterate thru the list, this recipe is
fine for my needs. I also learned about the .setdefault method -
thanks!
On 12 Dec 01, at 19:00, Jason Orendorff wrote:
> > 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
More information about the Python-list
mailing list