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:带一个参数的函数(排序时,会依次传入列表的每一 ...
随机推荐
- 启动项目时出现java.io.EOFException异常。
错误: 2018-4-18 10:55:54 org.apache.catalina.session.StandardManager doLoad 严重: IOException while load ...
- bzoj2440
题解: 莫比乌斯反演 ans=sigma(x/(i*i)*miu[i]) 代码: #include<bits/stdc++.h> using namespace std; ; int T, ...
- OO Summary Ⅳ
测试与正确性论证的效果差异 测试,或者说用断言进行黑箱测试,用大量的数据进行“覆盖性测试”,目的是当分支覆盖率达到100%也就是理论上来说所有可能的输入都已经测试过了,而输出结果均是正确的,那么我们理 ...
- Oracle 12c的自增列Identity Columns
在Oracle的12c版本中,Oracle实现了类似MySQL中的auto_increment的自增列,下面我们看一起Oracle是怎么实现的. Oracle Database 12c Enterpr ...
- 如何从零安装Mysql
1.yum/rpm安装 2.采用二进制方式免编译安装MySQL. 3.考虑到MySQL5.4.xx及以后系列产品的特殊性,其编译方式和早期的第一条产品线的有所不同,这里采用cmake或gmake方式的 ...
- 1085 PAT单位排行
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10^5),即考生人数.随后 N 行,每行按下列格式给出一个考 ...
- Java总结篇系列:Java泛型(转)
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- 从Oracle数据库中的本地命名文件tnsnames.ora来看服务别名、服务名和实例名的区别。
tnsnames.ora的作用这里就不多述了,各位应该都知道. 首先先看两个例子: test1 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCO ...
- Python 基础的应用day2
1 用户交互input,将用户输入的内容赋值给 name 变量 后只能是字符串str. 区别2和3: ps :python2:raw_input python3:input 例 :1 nam ...
- ChainingHash
public class ChainingHash<Key,Value>{ private int N; private int M; private doublylinked<Ke ...