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.
-----Original Message-----
From: python-list-bounces+jwhite=casl.umd.
@python.org [mailto:python-list-bounces+jwhite=casl.umd.
@python.org] On Behalf Of Gabriel Genellina
Sent: Wednesday, June 06, 2007 5:38 PM
To: python-l
@python.org
Subject: Re: creating lists based on parsed items
En Wed, 06 Jun 2007 13:24:54 -0300, Jason White <jwh@casl.umd.edu>
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