按照2元组中元素的比例对列表L进行排序

时间:2017-06-30 18:53:41

标签: python python-2.7 list sorting

我试图按照2元组中元素的比例对列表L进行排序。

Parameters
----------
L : {list} of 2-tuples ({tuple}) of {int}

Returns
-------
None   

Example
-------
>>> L = [(2, 4), (8, 5), (1, 3), (9, 4), (3, 5)]
>>> sort_by_ratio(L)
>>> L
[(1, 3), (2, 4), (3, 5), (8, 5), (9, 4)]

到目前为止我已经

L[:] = sorted(L,key = lambda ratio: ratio[0]/ratio[1])

但它以某种方式给了我一个[(2, 4),(1, 3),(3, 5), (8, 5), (9, 4)]

的列表

我哪里做错了?

1 个答案:

答案 0 :(得分:1)

L = [(2, 4), (8, 5), (1, 3), (9, 4), (3, 5)]
L[:] = sorted(L,key = lambda ratio: 1.*ratio[0]/ratio[1])
print L