Thanks ... Seems just like perl's list-in-a-hash (I've read some of the 
archives and no, I'm not seeking to incite a perl-vs-python flame war). I 
thought of doing this initially (and would have done via Perl), but didn't 
quite have a grip on associative arrays in python yet. Thanks for the input.

I've since stepped back and resolved the issue in a different way. The 
'buckets' were place holders to go into separate files. It was easier to just 
open the files in append mode and use the bucket name var to sort them straight 
into files. Was sort of a 'coulda had a V-8' moment. Wrapped it in a shell 
script that makes sure the files are removed at the outset.

Many thanks,

Jason


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gabriel Genellina
Sent: Wednesday, June 06, 2007 5:38 PM
To: python-list@python.org
Subject: Re: creating lists based on parsed items

En Wed, 06 Jun 2007 13:24:54 -0300, Jason White <[EMAIL PROTECTED]>
escribió:

> I am trying to do what (I think) should be fairly straightforward. I 
> have a list of items that i want to sort into buckets. in item 1 of 
> each line is the object I want to sort and in item 2 is the name of 
> the bucket. I am making it dynamic however (so I don't know the bucket 
> names in advance).

Forget about eval!
Use a dictionary: keys are bucket names, values a list containing all 
associated items. You will find the setdefault method very useful.

d = {}
for each item, do: d.setdefault(bucket, []).append(item)

If you are using Python 2.5, use a defaultdict instead, the very first example 
looks like what you want.
<http://docs.python.org/lib/defaultdict-objects.html>

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to