Sorting a list based on another list with DSU

220 views Asked by At

For a class project, I need to use python 2.1, and I'll probably have to use DSU for sorting, which I'm not familiar with. So I'd like some help if possible.

I have a list for storing vertexes and their coordinates. for example

vertex = [(2, 2), (3, 3), (1, 1)]

and so on.

I also have another list which will store the index positions of the vertex, sorted according to their first coordinate. So it will start like this

order = [0, 1, 2]

And my objective is for it to be like this, considering the example

order = [2, 0, 1]

without changing the order in the list "vertex".

Any ideas? Thanks in advance

1

There are 1 answers

1
Robᵩ On BEST ANSWER

I don't know if it works in Python2.1, but either of the following does what you ask in Python2.7:

order = [i for i,_ in sorted(enumerate(vertex), key = lambda x: x[1])]

or

order = [0,1 ,2]
order.sort(key=lambda x: vertex[x])

This probably works in Python2.1 (I don't have Python 2.1 installed to test it).

order.sort(lambda x,y:cmp(vertex[x],vertex[y]))