Sorting an Edge List

Anthony D'Agostino gamma-ray at dope.comcast.net
Fri Apr 29 17:22:45 EDT 2005


I need to sort this list:
[('A','Y'), ('J','A'), ('Y','J')] like this:
[('A','Y'), ('Y','J'), ('J','A')].

Note how the Ys and Js are together. All I need is for the second element of 
one tuple to equal the first element of the next tuple. Another valid 
solution is [('J','A'), ('A','Y'), ('Y','J')].

I was successful in doing this a while back with a modified bubble sort 
algorithm, but now I can't find my own code. Can the list be sorted with a 
comparison function or some other way?






More information about the Python-list mailing list