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 一些容易忽视的小点-类和对象
构造器 通过new关键字调用 构造器虽然有返回值,但是不能定义返回值类型(返回值的类型肯定是本类),不能在构造器里使用return返回某个值. 构造器是有权限的,也就是可以添加public,也可以添加 ...
- react中用pace.js
pace.js不支持npm, 所以只能直接下载下来,当作普通js引入 我在用的时候怎么都引不到组件里去用 后来终于找到方法了,直接上图了 1.先将pace文件下载来放在公共js目录下,pace.les ...
- 图片加载------reactVirtualized
作用: 让HTML文档始终保持固定数量的图片数量,可以节省带宽
- tomcat启动超时_tomcat was unable to start within
参考: http://jingyan.baidu.com/article/64d05a025c9969de55f73b23.html 首先,你得确认下你的数据库连接,尤其是在多个服务器之间转换或者服务 ...
- Oracle Rman 控制RMAN的备份时间,减少IO消耗
一.问题描述 由于服务器配置不高,备份策略为周末全备.周一至周六差异备份. 平时服务器CPU使用30%左右. 全备份时,开启两个通道,CPU达到70%-80%左右,业务不卡顿.不掉单,session不 ...
- ActiveMQ的发布者/订阅者模型示例
ActiveMQ的发布者/订阅者模型入门示例 (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878. ...
- 《Python》网络编程之客户端/服务端框架、套接字(socket)初使用
一.软件开发的机构 我们了解的涉及到两个程序之间通讯的应用大致可以分为两种: 第一种是应用类:QQ.微信.网盘等这一类是属于需要安装的桌面应用 第二种是web类:比如百度.知乎.博客园等使用浏览器访问 ...
- tf.nn.conv2d
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) input: 指需要做卷积的输入图像,它 ...
- 文件上传FormData
<div class="modal-dialog"> <div class="modal-content"> <div class ...
- 开发中最好使用not exists 取代not in
开发中使用not in可能会存在致命的错误,在子查询中,如果存在空值,not in返回的数据就是空了,如下创建2张数据表: user表: 部门表: 现在要查询没有分配到用户的部门有哪些,使用not i ...