python3 sort

#https://docs.python.org/3.5/howto/sorting.html?highlight=sort

#In Python 3.2, the functools.cmp_to_key() function was added to the functools module in the standard library

 

 

# compare function

def mycmp(x,y):
    if len(x) == len(y):
        if x== y :
            return 0
        elif x < y:
            return -1
        else:
            return 1
    else:
        return len(x) - len(y)

#transfer compare func to key
def cmp_to_key(mycmp):
    '''Convert a cmp= function into a key= function'''
    class K(object):
        def __init__(self, obj, *args):
            self.obj = obj
        def __lt__(self, other):
            return mycmp(self.obj, other.obj) < 0
        def __gt__(self, other):
            return mycmp(self.obj, other.obj) > 0
        def __eq__(self, other):
            return mycmp(self.obj, other.obj) == 0
        def __le__(self, other):
            return mycmp(self.obj, other.obj) <= 0
        def __ge__(self, other):
            return mycmp(self.obj, other.obj) >= 0
        def __ne__(self, other):
            return mycmp(self.obj, other.obj) != 0
    return K

#first do unique
line_list = list(np.unique(line_list))

#sort the string list
res = line_list.sort(key = cmp_to_key(mycmp))

转载于:https://www.cnblogs.com/squirrel2300/p/6716302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值