How to do arithmetic to sort a list of lists

I have a list of lists consisting of two numbers.

[[2, 3], [7, 8], [3, 5]] 

I would like to sort them based on the division of each of them:

e.g. 2/3 (0.666), 7/8 (0.875) 3/5 (0.6) to output:

 [[3, 5], [2, 3], [7, 8]] 

I suppose that somehow I will use lambda, but I do not know how to write it correctly. Something like this, but it just sorted by values:

 list_of_lists.sort(key=lambda x: (x[0],x[1])) 

How to do arithmetic?

+5
source share
2 answers
 lists = [[2, 3], [7, 8], [3, 5]] lists.sort(key=lambda x: (x[0]/x[1])) print(lists) 
+10
source

May this help you.

 a=[[2, 3], [7, 8], [3, 5]] print (sorted([a[i][0]/a[i][1] for i in range(len(a))])) 
-1
source

Source: https://habr.com/ru/post/1270766/


All Articles