Sorting x lists based on one list ... maybe an example would make sense:
Steven Bethard
steven.bethard at gmail.com
Wed May 18 16:58:23 EDT 2005
Ron Adam wrote:
> You can sort your grades list however you want. If you want to sort by
> student name instead of student_id, you would use:
>
> # Sort grades list by student name.
> grades.sort(lambda x,y: cmp(students[x[1]][0], students[y[1]][0]))
>
> Assuming the name is in the first field in the student dictionary-value
> tuple. There are probably other ways to do this that are more readable
> or faster.
Assuming that students[x[1]][0] is what you want to sort on, this may
also be written as:
grades.sort(key=lambda x: students[x[1]][0])
It will probably be somewhat faster, but depending on the size of your
data, you may not notice.
STeVe
More information about the Python-list
mailing list