Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

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

> 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

Uh-oh..
I am using Python 2.5 on WinXP, but when I tried the examples in the
manual, I got a "NameError: name 'defaultdict' is not defined".  What
am I missing in my Python installation?

-Basilisk96

On Wed, 06 Jun 2007 22:49:27 +0000, Basilisk96 wrote:
>> 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

> Uh-oh..
> I am using Python 2.5 on WinXP, but when I tried the examples in the
> manual, I got a "NameError: name 'defaultdict' is not defined".  What
> am I missing in my Python installation?

This would be a RTFM moment :) defaultdict is not a built-in, it is part
of the collections module.

You have to import collections first.

>>> defaultdict

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'defaultdict' is not defined
>>> import collections
>>> collections.defaultdict

<type 'collections.defaultdict'>

--
Steven.

> This would be a RTFM moment :) defaultdict is not a built-in, it is part
> of the collections module.

Bingo! I should have read higher up in the manual tree :)
Thanks
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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc