python中的排序
今天在http://www.pythontip.com刷题的时候遇到一个排序的问题:一个列表中既有字符串,又有数字,该怎么排序。
list = [1,2,5,4,'d','s','e',45]
list.sort()
如果直接调用sort()函数则会报
TypeError Traceback (most recent call last)
<ipython-input-9-f338e0e85925> in <module>()
----> 1 list.sort() TypeError: '<' not supported between instances of 'str' and 'int'
我理解的是sort()函数内部是通过'<'来完成大小的比较,而'<'不支持对字符串和数字之间的的比较。
后来发现有个sorted()函数可解决字符串和数字一起的排序问题
new_list = sorted(list)
sorted()跟sort()的调用方式不太一样,sorted()是将欲排序的list作为参数传入,然后得到排序后的list,而sort()是在原list的基础上进行排序。
sort()函数仅定义在list中,而sorted()对所有的可迭代对象都有效。
通过help()查看下两者区别:
---------------------------------sorted----------------------------------------
In [14]: help(list.sort)
Help on built-in function sort:
sort(...) method of builtins.list instance
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
--------------------------------sorted---------------------------------------
In [15]: help(sorted)
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.
升序返回一个新的列表包含所有项目的迭代。
可以提供自定义key函数以自定义排序顺序,可以设置反向标志以按降序返回结果。
python中的排序的更多相关文章
- python中字典排序,列表中的字典排序
python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...
- python中字典排序
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- Python中的排序方法sort(),sorted(),argsort()等
python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...
- Python中经典排序方法
数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...
- python中实现排序list
作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的python代码实现: from Node import * class OrderedList: def __init__(self) ...
- python中自定义排序函数
Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...
- Python中的排序方法
1 list.sort list.sort(key=None, reverse=False) 该方法只能用于list.就地排序,原来的list被修改.key的用法见下文.reverse控制降序还是生序 ...
- python中列表排序,字典排序,列表中的字典排序
#-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...
- python中的排序函数
1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1) cmp参数 cmp接受一 ...
随机推荐
- testNG框架提示:Cannot find class in classpath: NewTest
selenium+Java的testNG运行时,报如下错误: org.testng.TestNGException: Cannot find class in classpath: NewTest a ...
- fgetc read缓冲机制区别
read属于系统调用,它的缓存是基于内核的缓冲,是记在内核空间的. 而fgetc是标准函数, 是在用户空间I/O缓冲区的 比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O ...
- 基于麒麟座开发板2.0的MQTT实现例程
链接--->https://sanwen8.cn/p/649shZ1.html OneNET现已全面适配标准MQTT协议,相信这一功能的增加会**便于开发者进行设备的接入. OneNET提供了M ...
- PHP创建文件以及移动文件
创建文件,这里用到的是fopen,即是打开,又是创建 <?php $counter_file = 'aa.txt ';//文件名及路径,在当前目录下新建aa.txt文件 $fopen = fop ...
- php学习十三:其他关键字
在php中,其实不止在php中,在其他语言中我们也会常常接触到一些关键字,整理了一下php当中的一下关键字,可能有些不全,希望大家指出来,多多交流,一起进步. 1.final 特性:1.使用final ...
- Django restframwork
REST介绍 全称Representational State Transfer,即表现层状态转换,如果一个架构符合REST原则,我们就称他为Restfull架构,其主要包括如下方面: 资源Resou ...
- 解决报错:scandir() has been disabled for security reasons
服务器环境: LNMP 在服务器部署代码时候.遇到了这个问题. 蛋疼啊! 2 解决办法: 打开phpinfo.php , 搜索: scandir 找到disabled_function,确认此函数未 ...
- VIM 插入
不知道有多少VIM新手和我当年(去年)一样,信誓旦旦的以为只有i可以插入 唉,现在想想都觉得可笑,都是Windows下的编辑器用多了的结果 鼠标一点,妈妈再也不用担心我的文本插入了……悲剧! 好了,让 ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
- 深刻理解 React (一) ——JSX和虚拟DOM
版权声明:本文由左明原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/155 来源:腾云阁 https://www.qclou ...