Sorting a list
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Feb 1 15:16:31 EST 2007
John Salerno a écrit :
> Hi everyone. If I have a list of tuples, and each tuple is in the form:
>
> (year, text) as in ('1995', 'This is a citation.')
>
> How can I sort the list so that they are in chronological order based on
> the year?
Calling sort() on the list should just work.
> Is there a better way to do this than making a list of tuples?
Depends...
> (So far I have a text file and on each line is a citation. I use an RE
> to search for the year, then put this year and the entire citation in a
> tuple, and add this tuple to a list.
You don't tell how these lines are formatted, but it's possible that you
don't even need a regexp here. But wrt/ sorting, the list of tuples with
the sort key as first element is one of the best solutions.
More information about the Python-list
mailing list