python 排序 由大到小
Posted sea-stream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 排序 由大到小相关的知识,希望对你有一定的参考价值。
import functools class Solution: # @param {integer[]} nums # @return {string} def largestNumber(self, nums): def comparator(x, y): # inputs are string representations of non-negative ints if x+y > y+x: # no need to convert to int because x+y and y+x are same length return -1 # so lexicographic string sort behaves like numeric sort else: return 1 nums = list(map(str, nums)) # convert to strings nums.sort(key=functools.cmp_to_key(comparator)) return nums ls=[1,6,4,9,2,8] x=Solution() print(x.largestNumber(ls))
输出
[‘9‘, ‘8‘, ‘6‘, ‘4‘, ‘2‘, ‘1‘] [Program finished]
以上是关于python 排序 由大到小的主要内容,如果未能解决你的问题,请参考以下文章