On Fri, 29 Apr 2005 23:37:39 -0400, "Anthony D'Agostino" <[EMAIL PROTECTED]>
wrote:
>I found my old bubble sort solution:
>
>
>def esort(edges):
>while 1:
>swaps = 0
>for j in range(len(edges)-2):
>if edges[j][1] != edges
I found my old bubble sort solution:
def esort(edges):
while 1:
swaps = 0
for j in range(len(edges)-2):
if edges[j][1] != edges[j+1][0]:
edges[j+1],edges[j+2] = edges[j+2],edges[j+1] # swap
In article <[EMAIL PROTECTED]>,
"Anthony D'Agostino" <[EMAIL PROTECTED]> wrote:
> 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 t
Sort demands a unique ordering, which isn't present in your case.
You're constructing an Eulerian path. See Fleury's algorithm:
http://en.wikipedia.org/wiki/Eulerian_path
--
http://mail.python.org/mailman/listinfo/python-list
Anthony D'Agostino, this is my first raw version, it can fail in lots
of ways depending on your input list l (and surely there are better
ways to do it), but it's a start:
. l = [('A','Y'), ('J','A'), ('Y','J')]
. pairs = dict(l)
. result = []
. key = pairs.iterkeys().next()
. while pairs:
. v
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'