[Tutor] globbing and sorting and dating, oh my!

Anna Ravenscroft anna at aleax.it
Wed Feb 4 15:37:12 EST 2004


On Wednesday 04 February 2004 06:55 pm, Kirk Bailey wrote:

> If the dates are a list, and I sort the list, the corresponding list
> of filenames no longer corresponds. THIS is an issue which looms large
> in my army surplus used brain.

I'd consider using a dict. Python dicts are fast, easy to use, and highly 
useful.

mydict = {key1:value1, key2:value2}

You can use dates as the keys (and any lists or strings or combination thereof 
as the values), pull a list of the keys using d.keys(), sort them, remove any 
that are old, then use the remaining list to pull the corresponding values 
into a new dict or list for display. 

Or, use a list comprehension directly on the dict to get a list of new items 
(since you don't really *have* to sort them by date, do you?). It'll give you 
a new list of tuples, which you can pass to a new dict if you want to:

newstuph = dict( [(date, mydict[date]) for date in mydict if date > olddate] )

Hope this helps. 

Anna

-- 
There is a type 3 error in which your mind goes totally blank whenever you try 
to remember which is which of type 1 and type 2.
                                                 -Richard Dawkins

-- 
There is a type 3 error in which your mind goes totally blank whenever you try 
to remember which is which of type 1 and type 2.
                                                 -Richard Dawkins




More information about the Tutor mailing list