sorted
排序是编程中经常使用到的算法,无论哪种排序算法, 本质上都是比较两个元素的大小。如果是数字,可以直接比较,但是如果是字符串或者是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的更多相关文章
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- 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 ...
- 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 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [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 ...
- [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 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- [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 ...
- [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. 这道 ...
随机推荐
- [SNOI2017]炸弹
嘟嘟嘟 这题有一些别的瞎搞神奇做法,而且复杂度似乎更优,不过我为了练线段树,就乖乖的官方正解了. 做法就是线段树优化建图+强连通分量缩点+DAGdp. 如果一个炸弹\(i\)能引爆另一个炸弹\(j\) ...
- sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file
sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file 1. 权限问题 ...
- ansible roles
roles 特点 目录结构清晰 重复调用相同的任务 目录结构相同 web - tasks - install.yml - copfile.yml - start.yml - main.yml - t ...
- 我的工具:Ping工具
C# Ping工具 通过该工具可以多个地点Ping服务器以检测服务器响应速度,同时也可以测试网站的响应速度,解析时间,服务器连接时间,下载速度 工具下载地址:https://download.csdn ...
- ORM简介
ORM就是object relational mapping,对象关系映射. 将关系型数据库转化为对象来进行处理. 数据表就是一个类,表的一行就是一个对象,一行的每个字段就是属性. 忽然想到了在MVC ...
- c#使用资源文件完成国际化
路径结构如下 namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void Te ...
- windows常用快捷键和指令
快捷键: Ctrl+鼠标滚轮:更改图标大小(桌面).缩放(开始屏幕) Ctrl+A:选择所有 Ctrl+C:复制 Ctrl+E:选择搜索框(资源管理器) Ctrl+N:新窗口(资源管理器) Ctrl+ ...
- debian apt sources
deb http://debian.csie.ntu.edu.tw/debian/ sid main contrib non-free deb-src http://debian.csie.ntu.e ...
- Linux下MySql的登陆和管理操作
一.mysql数据库启停1.linux下启动mysql的命令: mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径)2.linux下重启 ...
- [SCOI2009]生日礼物题解
题目 一道模拟和队列题,但模拟比队列的成分多一些.队列也就是用两个指针模拟的. 可以用枚举的思想.首先我们知道r(即区间的右端点是肯定不会左移的),而l右移的同时,r可能不变,也可能右移,所以这样就可 ...