Py学习  »  Python

使用键参数对python中的列表排序

Steven • 5 年前 • 1715 次点击  

我想根据以下语句对元素列表进行排序:

i>=j  if(i_rank < j_rank OR ((i_rank == j_rank) AND (i_dinstance > j_dinstance)))

我需要如何在python 3中使用sort()函数来声明这一点?

我知道我可以使用key参数,然后创建一个如下的函数:

   def sorter(x):
    if(i.rank < j.rank or ((i.rank == j.rank) and (i.distance > j.distance))):
        return ??
    else: 
        return ??

list.sort(reverse=True, key = sorter)

但我不知道sorter函数的参数是什么?函数的返回呢?

谢谢:-)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46862
 
1715 次点击  
文章 [ 2 ]  |  最新文章 5 年前