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:带一个参数的函数(排序时,会依次传入列表的每一 ...
随机推荐
- Mysql InnoDB三大特性-- double write
转自:http://www.ywnds.com/?p=8334 一.经典Partial page write问题? 介绍double write之前我们有必要了解partial page write( ...
- day02 运算符和编码
今日所学 主要是运算符和编码的初认识, 1 还有比较运算 ==,!=,<>,>,<,>=,<=等 2 . 赋值运算 =,+=,-=等 还有今天的难点逻辑运算 ...
- CMOS集成门电路
CMOS集成门电路:mos管构成的集成门电路 CMOS:互补对称金属氧化物半导体器件 CMOS反相器电路是由N沟道MOSFET和P沟道MOSFET互补而成:特点静态功耗近视为0,电源电压可在很宽的范围 ...
- 4.3 C++虚成员函数表vtable
参考:http://www.weixueyuan.net/view/6372.html 总结: 在C++中通过虚成员函数表vtable实现多态,虚函数表中存储的是类中虚函数的入口地址. 使用多态会降低 ...
- OJ_查找二叉树
#include<iostream>using namespace std;int n,m;int d[120];int t=1;int re;struct Node{ int data; ...
- java学习笔记19(Arrays类)
Arrays类: 此类包含用来操作数组的各种方法(比如升序和搜索): import java.util.Arrays; public class Demo { public static void m ...
- POJ 2001 Shortest Prefixes(字典树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- 微软Power BI 每月功能更新系列——11月Power BI 新功能学习
Power BI Desktop11月产品功能摘要 本月Power BI Desktop 有一个大规模的更新.现在,通常可以使用复合模型在一个模型中将直接查询和导入源组合在一起.UserVoice上的 ...
- [转]熵(Entropy),交叉熵(Cross-Entropy),KL-松散度(KL Divergence)
https://www.cnblogs.com/silent-stranger/p/7987708.html 1.介绍: 当我们开发一个分类模型的时候,我们的目标是把输入映射到预测的概率上,当我们训练 ...
- nginx php
server { listen 443; server_name www.awkj.com; ssl on; ssl_certificate cert/214683879970617.pem; ssl ...