Answer the question
In order to leave comments, you need to log in
How to sort a list nicely in Python?
Task:
There is a list of pairs of elements:
compound_transpositions =
There are coefficients of a linear function:
coefficients = [17, 69, 84, 3 , 46, 97, 12, 68, 70, 10]
We need to sort the compound_transpositions by increasing the difference of the corresponding coefficients of the linear function.
We managed to get the differences themselves, for example:
keys = map(lambda x: T.coefficients[x[0]]-T.coefficients[x[1]], p.compound_transpositions())
>>>[81, -43, -51, 85]
That is, the first pair [2, 3] corresponds to the difference between the second and third coefficients of the function: 84-3=81. Etc.
Question: how to combine it now. The main thing: the task is simple and clear how to do it algorithmically. But not just a solution, but a beautiful solution is of interest. Python style, one line.
True Madskills hackers, tell me please. Thanks in advance!
Answer the question
In order to leave comments, you need to log in
>>> compound_transpositions =
>>> coefficients = [17, 69, 84, 3, 46, 97, 12, 68, 70, 10]
>>> compound_transpositions.sort(key=lambda x: coefficients[x[0]]-coefficients[x[1]])
>>> compound_transpositions
What makes you think Python style is one line? Pythonists, on the contrary, do not like such lines.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question