排序是编程中经常使用到的算法,无论哪种排序算法, 本质上都是比较两个元素的大小。如果是数字,可以直接比较,但是如果是字符串或者是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. js中innerHTML、outerHTML与innerText的用法与区别

    ____________________________________________________________________________________________________ ...

  2. 如何将div高度填满剩余高度

    下列代码中navbar高度为30px,content高度需要填满浏览器的剩余高度 <div id="body">     <div id="navbar ...

  3. Playfair 加密

    题目真的好长但是意思很简单 89.加密 (15分)C时间限制:3 毫秒 | C内存限制:3000 Kb题目内容:一种Playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重 ...

  4. MongoDB之常用操作

    最近经常使用MongoDB来进行数据的操作,特此记录总结一下

  5. 关于vue移动端的适配

    http://blog.csdn.net/z1712636234/article/details/77881685

  6. 【AGC030F】Permutation and Minimum DP

    题目大意 有一个长度为序列 \(a\),其中某些位置的值是 \(-1\). 你要把 \(a\) 补成一个排列. 定义 \(b_i=\min(a_{2i-1},a_{2i})\),求有多少种可能的 \( ...

  7. cyq.data 常见使用方法

    配置数据库链接:(这只是其中一种方式) AppConfig.DB.CommandTimeout = 800; AppConfig.DB.DefaultConn = "数据库链接地址" ...

  8. ZOJ 3949 Edge to the Root( 树形dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一 ...

  9. python之路day12--装饰器的进阶

    装饰器# 开发原则:开发封闭原则# 装饰器的作用:在不改变原函数的调用函数下,在函数的前后添加功能.# 装饰器的本质:闭包函数 import time def timmer(f): #func #ti ...

  10. CodeForces - 597C Subsequences (树状数组+动态规划)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...