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))
python3 sort的更多相关文章
- python3 sort list
1. 对元素指定的某一部分进行排序,关键字排序 s = ['release.10.txt','release.1.txt','release.2.txt','release.14.txt','rele ...
- python outline
1.列表/数组/numpy/Pandas Python list 初始化技巧 (2018-12-27 11:54) python3 sort list (2019-05-23 14:52) P ...
- Python3:sorted()函数及列表中的sort()函数
一.sort,sorted函数介绍: Sort函数是list列表中的函数,而sorted可以对list或者iterator进行排序. 下面我们使用help来查看他们的用法及功能: sort: ...
- 在使用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 ...
- python3.x 匿名函数lambda_扩展sort
#匿名函数lambda 参数: 表达式关键字 lambda 说明它是一个匿名函数,冒号 : 前面的变量是该匿名函数的参数,冒号后面是函数的返回值,注意这里不需使用 return 关键字. ambda只 ...
- Python3基础 sort(reverse=True) 将一个列表降序排列
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Python3基础 sort 将一个列表中的值升序排列
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Python3:排序函数sort() 和 sorted() 之介绍
今天来讲一下Python中的排序函数.Python中有2个内建的排序函数,分别为sort() 和 sorted() 下面介绍分别介绍一下2个函数: 1.有一个列表 :a=[1,4,5,88,0,7], ...
- sort、sorted高级排序-Python3.7 And 算法<七>
1.sort(*, key=None, reverse=False) sort()接受两个参数,这两个参数只能通过关键字(关键字参数)传递. 参数key:带一个参数的函数(排序时,会依次传入列表的每一 ...
随机推荐
- centos6.5 安装php-5.6.31
1 从PHP官网下载所需要的PHP版本 下载地址: http://php.net/get/php-5.6.31.tar.gz/from/a/mirror 把下载好的文件上传到服务器 2 安装PHP ...
- hdu1693
题解: 还是插头dp 代码: #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- python使用变量
#不建议用加号,建议用.format name = input('name:') age = input('age:') print( name ,age) print('姓名:',name,'年龄: ...
- Linux学习 : Socket 网络编程入门
一.socket()函数 int socket(int domain, int type, int protocol); domain:即协议域,又称为协议族(family).常用的协议族有,AF_I ...
- 4.3 C++虚成员函数表vtable
参考:http://www.weixueyuan.net/view/6372.html 总结: 在C++中通过虚成员函数表vtable实现多态,虚函数表中存储的是类中虚函数的入口地址. 使用多态会降低 ...
- Problem A: 编写函数:三个数的最大最小值
Description 给出三个数a,b,c,最大值是?最小值是? ------------------------------------------------------------------ ...
- Aizu - 2681(括号匹配)
Problem Statement You are given nn strings str1,str2,…,strnstr1,str2,…,strn, each consisting of ( an ...
- shell脚本实例-判断主机存活 以及企业备份方案
1.上次写了一个脚本我那次考虑不是很周全,这次我将脚本改动了一下,这次是判断三次, 希望关注我的人可以经常交流哈.下面我写上代码. #!/usr/bin/bash while read ip do f ...
- 2019-03-07-day006-小数据池
01 昨日内容回顾 字典: 映射,{} 键值对的形式存储,容器型数据类型,key 唯一的,可哈希的,value任意数据类型,对象. 3.6之前无序的, 3.6之后,有序的(第一次创建字典的顺序) 特点 ...
- c# GetType()和typeof()的区别
c# GetType()和typeof()的区别 C#中任何对象都具有GetType()方法,返回Type类型的当前对象的类型. GetType()是基类System.Object的方法,因此只有 ...