list organization question

Joe Strout joe at strout.net
Thu Dec 11 18:21:47 EST 2008


On Dec 11, 2008, at 4:12 PM, Robocop wrote:

> I have a list of objects, each object having two relevant attributes:
> date and id.  I'd like not only organize by id, but also by date.
> I.e. i would like to parse my list into smaller lists such that each
> new mini-list has a unique date, but consists of only objects with a
> specific id.

I'm not quite following you here.  What are the inputs and outputs  
you're after?

For the sake of argument let's say you want: given the list of objects  
(let's call it mylist), and a desired ID (desired_id), return a  
smaller list of all the objects with that ID.  That would be simply:

   return [item for item in mylist if item.id=desired_id]

Look up "list comprehensions" for an explanation of what's going on  
here.

If you want a list for a given date, then just change the "if" part.

You seem to want both at once, but I can't understand what that means.

HTH,
- Joe





More information about the Python-list mailing list