码迷,mamicode.com
首页 > 编程语言 > 详细

python 排序 由大到小

时间:2019-03-29 10:33:52      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:on()   ring   input   graph   int   ini   use   elf   graphic   

 

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 排序 由大到小

标签:on()   ring   input   graph   int   ini   use   elf   graphic   

原文地址:https://www.cnblogs.com/sea-stream/p/10619450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!