python - how to sort

  1. overview

  2. Key function (★★★★★)

  3. OPerator module functions

  4. asc and desc 升序和降序


Overview

对于python 列表,有一个方法 list.sort()  ,另外还有一个内置函数sorted()

list.sort() 是对本身排序,不会产生新的对象。而sorted 接收一个可迭代对象,返回一个新的排好序的list

Help on built-in function sorted in module builtins:

sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order
>>> help(list.sort)
Help on method_descriptor: sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

list.sort() 会把原始列表修改,return None ,当你不需要原始列表的时候,这个方法会更有效率。

>>> a=[3,5,2,1]
>>> a.sort()
>>> a
[1, 2, 3, 5]

sorted()  这个方法用起来比较方便

 >>> sorted([6,3,8,12,4])
[3, 4, 6, 8, 12]
>>>

sorted() 接收可迭代对象

eg.

比如

>>> dic={4:'a',2:'b',3:'A',1:'h'}
>>> sorted(dic)
[1, 2, 3, 4]

Key function

Both list.sort() and sorted() have a key parameter to specify a function to be called on each list element
prior to making comparisons

list.sort()和sorted()都有一个关键参数来指定在每个列表元素上被调用的函数在进行比较之前。

for example:

>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

The value of the key parameter should be a function that takes a single argument and returns a key to use
for sorting purposes. This technique is fast because the key function is called exactly once for each input
record

key 参数的值应该是一个函数对象,这个函数对象带一个参数,返回一个key,这个key 就是排序的标准,

class Student(object):
def __init__(self,name,grade,age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name,self.grade,self.age)) ls_grade = sorted([
Student('join',90,15),
Student('alex',87,13),
Student('eleven',100,17)
],key=lambda stu:stu.grade) ls_age = sorted([
Student('join',90,15),
Student('alex',87,17),
Student('eleven',100,14)
],key=lambda stu:stu.age) print(ls_grade)
print(ls_age)

OPerator module functions



asc and desc 升序和降序

默认是升序排序

reverse 默认是false ,如果是true ,那就是降序排列

sorted 和list.sort() 的排序是稳定排序

于洋 回到顶部

python - how to sort的更多相关文章

  1. python中的sort方法

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  2. python中的sort方法使用详解

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  3. Python中的sort()

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可 ...

  4. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  5. python sorted和sort

    我们需要对List进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序 方法2.用built-in函数sorted进行排序(从2.4开始) ...

  6. Python中的sort()方法使用基础

    一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)itera ...

  7. Python中的 sort 和 sorted

    今天在做一道题时,因为忘了Python中sort和sorted的用法与区别导致程序一直报错,找了好久才知道是使用方法错误的问题!现在就大致的归纳一下sort和sorted的用法与区别 1. sort: ...

  8. Simplest Python K-Way Merging Sort|最简单的Python k路归并排序

    想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...

  9. python 按照自然数排序遍历文件 python os.listdir sort by natural sorting

    import os import re def sorted_aphanumeric(data): convert = lambda text: int(text) if text.isdigit() ...

随机推荐

  1. 个人博客作业_week1

    1.<构建之法>的5个问题 1.如何避免在产品开发后期不断有重大修改,导致其他模块的连锁反应? 2.游戏用户有哪些类型? 3.如何衡量软件工程的质量? 4.怎么协调团队里相互间的任务分配? ...

  2. BF算法和KMP算法 python实现

    BF算法 def Index(s1,s2,pos = 0): """ BF算法 """ i = pos j = 0 while(i < ...

  3. git学习心得

    https://github.com/zhangxinn/test/tree/master 自己虽然在课堂上有认真的听老师讲解如何使用github,包括怎样在线学习,怎样在github上建立自己的仓库 ...

  4. 第三个Sprint ------第八天

    四则运算APP开发基本完成! PC端运行结果截图 移动端(华为手机)测试截图 总结:通过这次课程设计,我体会到团队协作的重要性,也体会到了理论联系实际的意义.做一件事,要坚持不懈,不能半途而废!

  5. SPRINT四则运算(第二天)

    1.小组成员: 李豌湄:master 江丹仪:产品负责人 2.现状: a.已经下载APP分析他们的界面.优缺点和闪光点  b.已改进代码添加功能 3.任务认领: 完成任务的第一个模块: a.下载五个类 ...

  6. Android动画总结

    本文总结常用属性方法等,详细学习可使用如下郭霖大神文章: Android属性动画完全解析(上),初识属性动画的基本用法 Android属性动画完全解析(中),ValueAnimator和ObjectA ...

  7. Alpha冲刺随笔汇总

    项目Alpha冲刺(团队) Alpha冲刺随笔汇总 姓名 学号 博客链接 何守成 031602408 http://www.cnblogs.com/heshoucheng/ 黄锦峰 031602411 ...

  8. HDU 2043 密码

    http://acm.hdu.edu.cn/showproblem.php?pid=2043 Problem Description 网上流传一句话:"常在网上飘啊,哪能不挨刀啊-" ...

  9. Debian中APT的前世今生

    https://baike.baidu.com/item/apt-get/2360755 https://www.debian.org/doc/manuals/debian-handbook/sect ...

  10. IDEA 操作及快捷键总结

    一.设置IDEA使用Eclipse快捷键 File->Settings->Keymap->选择Eclipse,就可以使用Eclipse的快捷键了,但是不能修改.如果想要修改,需要点击 ...