#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))

python3 sort的更多相关文章

  1. python3 sort list

    1. 对元素指定的某一部分进行排序,关键字排序 s = ['release.10.txt','release.1.txt','release.2.txt','release.14.txt','rele ...

  2. python outline

    1.列表/数组/numpy/Pandas Python list 初始化技巧   (2018-12-27 11:54) python3 sort list   (2019-05-23 14:52) P ...

  3. Python3:sorted()函数及列表中的sort()函数

    一.sort,sorted函数介绍:   Sort函数是list列表中的函数,而sorted可以对list或者iterator进行排序.   下面我们使用help来查看他们的用法及功能: sort: ...

  4. 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'

    使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...

  5. python3.x 匿名函数lambda_扩展sort

    #匿名函数lambda 参数: 表达式关键字 lambda 说明它是一个匿名函数,冒号 : 前面的变量是该匿名函数的参数,冒号后面是函数的返回值,注意这里不需使用 return 关键字. ambda只 ...

  6. Python3基础 sort(reverse=True) 将一个列表降序排列

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  7. Python3基础 sort 将一个列表中的值升序排列

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  8. Python3:排序函数sort() 和 sorted() 之介绍

    今天来讲一下Python中的排序函数.Python中有2个内建的排序函数,分别为sort() 和 sorted() 下面介绍分别介绍一下2个函数: 1.有一个列表 :a=[1,4,5,88,0,7], ...

  9. sort、sorted高级排序-Python3.7 And 算法<七>

    1.sort(*, key=None, reverse=False) sort()接受两个参数,这两个参数只能通过关键字(关键字参数)传递. 参数key:带一个参数的函数(排序时,会依次传入列表的每一 ...

随机推荐

  1. linux 定时任务 日志记录

    1  不记录日志 > /dev/null 2>&1 2 日志记录追加到指定文件 >> /path/mylog.log 2>&1

  2. xpath & <tr><td><br>

    python : 3.6 lxml : 4.2.1 from lxml.html import etree test_html = ''' <!DOCTYPE html PUBLIC " ...

  3. (C/C++学习笔记) 二十二. 标准模板库

    二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...

  4. 1043 输出PATest

    给定一个长度不超过 10​4​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种 ...

  5. Centos7初始配置

    配置 centos7 ip地址: vi /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=static ONBOOT=yes NM_CONTRO ...

  6. Python代码 注释

    对某些代码进行标注说明,增加程序的可读性. 一.单行注释 以“#” 开头,#后面的所有东西都不会被运行 print("hello python") # 输出 `hello pyth ...

  7. FMX取得屏分辨率

    procedure Tfrm_Main.FormCreate(Sender: TObject); var ScreenSvc: IFMXScreenService; Size: TPointF; be ...

  8. python-django优缺点

    [Django]是利用[Python]语言从事[Web]开发的首选框架.如果你以后想从事[python web]开发工作,就必需了解其优缺点.这些都可能会是你将来的面试题哦. [Django]的优点 ...

  9. 用XPath精确定位节点元素&selenium使用Xpath定位之完整篇

    在利用XSL进行转换的过程中,匹配的概念非常重要.在模板声明语句 xsl:template match = ""和模板应用语句xsl:apply-templates select ...

  10. Redis开机自启动

    1.新建redis文件 vi /etc/init.d/redis 2.修改/tec/init.d/redis文件,要修改Redis安装目录及配置文件路径 EXEC=/usr/local/redis/b ...