排序是编程中经常使用到的算法,无论哪种排序算法, 本质上都是比较两个元素的大小。如果是数字,可以直接比较,但是如果是字符串或者是dict呢?直接比较数学上的大小是没有意义的,因此,比较的过程必须通过函数抽象出来。

python内置的sorted()函数就可以对list进行排序

>>> sorted([1,-9,56,2,-6])
[-9, -6, 1, 2, 56]

此外,sorted()也是一个高阶函数,它还可以接受一个key函数来实现自定义的排序,例如按绝对值大小排序

>>> sorted([1,-9,56,2,-6],key=abs)
[1, 2, -6, -9, 56]

key指定的函数将作用于list的每一个元素上,并根据key函数返回结果进行排序。


再看字符串排序

>>> sorted(['Zoo','Andy','David','animal'])
['Andy', 'David', 'Zoo', 'animal']

默认情况下是按照ASCII码字典序排列的。由于Z的ASCII码小于a,所以以Z开头的词会排在a开头的词前面

如果我们要忽视字母大小写进行排序,可以将lower函数作为key传入

sorted(['Zoo','Andy','David','animal'],key=str.lower)
['Andy', 'animal', 'David', 'Zoo']

如果要进行反向排序,可以传入reverse=True

sorted(['Zoo','Andy','David','animal'],reverse=True)
['animal', 'Zoo', 'David', 'Andy']

从上述例子可以看出,高阶函数的抽象能力是非常强大的,而且,核心代码可以保持得非常简洁。

sorted的更多相关文章

  1. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  2. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  3. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  6. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  10. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

随机推荐

  1. June. 26th 2018, Week 26th. Tuesday

    No affection but interests can be found in the world of animals. 在动物的世界里,只有利益,没有感情. From Animal Worl ...

  2. C#行转列&绑定DGV

    c#行转列 今天工作中,恰好写到此处,想起之前第一次行转列的时候,卡壳了好久,今天正好碰上,故而花费几分钟,整理成案例,分享到博客上. 这是个很简单的功能,第一次可以使用案例,后面最好能达到信手拈来的 ...

  3. zcu102 hdmi example(二)

    1.概述 上篇说到,调用跑HDMI IP核自带的design example,跑出来的结果是显示屏显示彩条,并伴有嘀,嘀,嘀...的声音.因为在实际项目中,我们只需要图像,不需要声音的,所以我要把声音 ...

  4. 深度学习识别CIFAR10:pytorch训练LeNet、AlexNet、VGG19实现及比较(一)

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 前面几篇文章介绍了MINIST,对这种简单图片的识别,LeNet-5可以达到99%的识别率. CIFA ...

  5. A Base Class pointer can point to a derived class object. Why is the vice-versa not true?

    问题转载自:https://stackoverflow.com/questions/4937180/a-base-class-pointer-can-point-to-a-derived-class- ...

  6. CF 543C Remembering Strings

    https://cn.vjudge.net/problem/CodeForces-543C 题目 You have multiset of n strings of the same length, ...

  7. QProcess与外部程序的调用

    项目做到一定阶段,常常须要在原来的project上调用外部程序. Qt为此提供了QProcess类,QProcess可用于完毕启动外部程序,并与之交互通信. 一.启动外部程序的两种方式:(1)一体式: ...

  8. 洛谷P1197 [JSOI2008]星球大战

    题目 由于题目不要求强制在线,所以可以离线. 而离线的话就会带来许多便利,所以我们可以先处理出全部打击后的图,通过并查集来判断是否连通. 然后再从后往前枚举,得出答案 #include <bit ...

  9. 特殊计数序列——第二类斯特林(stirling)数

    计算式 \[ S(n,m)=S(n-1,m-1)+mS(n,m) \] \(S(0,0)=1,S(i,0)=0(i>0)\) 组合意义 将\(n\)个不可分辨的小球放入\(m\)个不可分辨的盒子 ...

  10. [openssh-server]install and enable ssh in debian9 / 在debian9上安装并启用ssh

    新安装的debian9.8 with XFCE 发现没有ssh,下载debian-9.8.0-amd64-DVD-1.iso并挂在到ESXi虚拟机/media/cdrom0. 清空或保存/etc/ap ...