今天在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. Android Timer的应用示例

    package com.hyzhou.timerdemo1; import java.util.Timer; import java.util.TimerTask; import android.os ...

  2. Python 入门(九)迭代

    什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...

  3. ContentPriver

    共享应用程序内的数据, 在数据修改时可以监听 1.特点 ①.可以将应用中的数据对外进行共享: ②.数据访问方式统一,不必针对不同数据类型采取不同的访问策略: ③.内容提供者将数据封装,只暴露出我们希望 ...

  4. HTML基础做出属于自己的完美网页

    HTML HTML解释: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的规则(W3C),大家都来 ...

  5. 【Laravel】Mac下玩转Laravel

    1 apache 首先Mac系统是自带了Apache,只需要执行 sudo apachectl start 就可以打开Apache服务,然后访问 http://localhost 就可以访问到,it' ...

  6. MUI 图标筛选切换(父页面传值子页面)

      1 父页面: index.html <li class="tab_layout"> <a href="javascript:;" clas ...

  7. 【CSS系列】对表单和数据表格应用样式

    表格特有的元素: 1.summary和caption caption用作与表格的标题.summary应用于表格标签,用来描述表格的内容,于image标签的alt文本相似. 2.thead tbody ...

  8. memcached与redis实现的对比

    版权声明:本文由田京昆原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/129 来源:腾云阁 https://www.qclo ...

  9. Fatal error Using $this when not in object context in

    大致意思是 $this 没有上下文,原因是没有对此类进行实例化. 出现此错误的原因是:在FileCommand.php中使用 $this->方法/属性. $this 不是不可以用,而是要看情况用 ...

  10. linux常用的一些访问目录

    救命三键(Ctrl+Alt+Delete)  /var/log     如果是网络服务的问题时,请到 这个目录里头去查阅一下 log file (登录档): /etc/rc.local    修改挂载 ...