今天在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中的排序的更多相关文章

  1. python中字典排序,列表中的字典排序

    python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...

  2. python中字典排序

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  3. Python中的排序方法sort(),sorted(),argsort()等

    python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...

  4. Python中经典排序方法

    数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...

  5. python中实现排序list

    作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的python代码实现: from Node import * class OrderedList: def __init__(self) ...

  6. python中自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  7. Python中的排序方法

    1 list.sort list.sort(key=None, reverse=False) 该方法只能用于list.就地排序,原来的list被修改.key的用法见下文.reverse控制降序还是生序 ...

  8. python中列表排序,字典排序,列表中的字典排序

    #-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...

  9. python中的排序函数

    1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1)  cmp参数 cmp接受一 ...

随机推荐

  1. Effective C++ Item 13 Use object to manage resources

    1. Always use object to manage resource! If you delete a pointer or release a handler manually by yo ...

  2. Bitmap之安卓手机壁纸的设置

    MainActivity: package com.hyzhou.imagedemo; import java.io.File; import android.os.Bundle; import an ...

  3. 关于直播学习笔记-004-nginx-rtmp、srs、vlc、obs

    1.采集端:OBS RTMP推流地址:rtmp://192.168.198.21:1935/live 流密钥:livestream(任意-但播放地址与此一致) 2.播放端:nginx-rtmp-win ...

  4. python2.0_s12_day9_协程&多线程和cpu,磁盘io之间的关系

    事件驱动和异步io有什么直接关系. 当我们访问一个网页,不考虑网络问题.我们人类不觉得网页慢. 但是实际中对计算机来说还是慢.那慢在哪里.io io操作是整个网络操作中最慢的.比如你打开网页要是有2秒 ...

  5. ftp简单命令

    1.连接ftp ftp 192.168.10.15 进去后输入用户名 ,然后再输入密码,就这样登陆成功了,你会看到 ftp> 2.进入ftp后,你对目录需要切换操作.和linux一样的命令.cd ...

  6. CRUX下实现进程隐藏(3)

    通过一个内核模块拦截文件系统的回调函数来实现进程隐藏. VFS(Virtual File System)是Linux在实际文件系统(如ext3,ext4,vfat等)上抽象出的一个文件系统模型,简单来 ...

  7. EUI组件之DataGroup

    看官网教程,这个没法单独用. http://developer.egret.com/cn/github/egret-docs/extension/EUI/dataCollection/dataGrou ...

  8. Eclipse初体验

    Eclipse有很多个版本: 这里我们下载Eclipse for javaEE版,既可以写javaSE代码又可以写web代码,省去了很多插件配置的时间.官网下载地址:http://www.eclips ...

  9. reflect 机制

    1: Class.forName的作用?为什么要用? 答:调用该访问返回一个以字符串指定类名的类的对象. 2: 通过反射,有几种方法可以实例化Class类对象? 3种,第一种:Class.forNam ...

  10. apache+tomcat实现session共享

    apache+tomcat上篇文章,实现了负载均衡,现在我们实现session共享 一.tomcat集群配置,session 同步配置: tomcat1配置  A.修改Engine节点信息: < ...