sort与sorted
Python list
内置sort()
方法用来排序,也可以用python内置的全局sorted()
方法来对可迭代的序列排序生成新的序列。
1.list.sort()
方法仅被定义在list
中,相反地sorted()
方法对所有的可迭代序列都有效。
2.使用方式: 使用sort只能list.sort(),不能sort(list)
使用sorted只能sorted(iteritems),不能iteritems.sorted
>>> sorted({'d': 1, 'a': 3,'b': 2}) 默认对key排序
['a', 'b', 'd']
>>> sorted({1:'q',3:'c',2:'g'}.keys())
[1, 2, 3]
>>> sorted({1:'q',3:'c',2:'g'}.values())
['c', 'g', 'q']
3.sort要改变原来的list
sorted返回一个新顺序的iteritems,但不会改变原来的iteritems
>>> a = [2,1,5]
>>> b = a.sort() 会发现这一步并没有对b进行赋值,在a排完序后在用b = a去赋值才成功
>>> print a
>>> [1,2,5]
>>> print b
>>> None
>>> b = a
>>> print b
>>> [1,2,5] >>> a = [2,1,5]
>>> b = sorted(a)
>>> b
>>> [1,2,5]
>>> a
>>> [1,2,5]
4.
>>> sorted([(2,1),(1,2),(3,4)])
[(1, 2), (2, 1), (3, 4)] sorted默认对第一个进行排序
sort与sorted的更多相关文章
- 测试对于list的sort与sorted的效率
sorted from time import clock from random import randint start = clock() a = [randint(0,1000000) for ...
- [python学习] 语言基础—排序函数(sort()、sorted()、argsort()函数)
python的内建排序函数有 sort.sorted两个. 1.基础的序列升序排序直接调用sorted()方法即可 ls = list([5, 2, 3, 1, 4]) new_ls = sorted ...
- python sort和sorted的区别以及使用方法
iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...
- Python中的 sort 和 sorted
今天在做一道题时,因为忘了Python中sort和sorted的用法与区别导致程序一直报错,找了好久才知道是使用方法错误的问题!现在就大致的归纳一下sort和sorted的用法与区别 1. sort: ...
- Python3:排序函数sort() 和 sorted() 之介绍
今天来讲一下Python中的排序函数.Python中有2个内建的排序函数,分别为sort() 和 sorted() 下面介绍分别介绍一下2个函数: 1.有一个列表 :a=[1,4,5,88,0,7], ...
- 剑指offer第32题:把数组排成最小的数及关于list.sort()和sorted( Iterable object )函数的相关知识
* 解题思路: * 先将整型数组转换成字符数组,然后将String数组排序,最后将排好序的字符串数组拼接出来.关键就是制定比较规则. * 排序规则如下: * 若ab > ba 则 a & ...
- python 排序 sort和sorted
当我们从数据库中获取一写数据后,一般对于列表的排序是经常会遇到的问题,今天总结一下python对于列表list排序的常用方法: 第一种:内建方法sort() 可以直接对列表进行排序 用法: list. ...
- 【Python】 sort、sorted高级排序技巧
文章转载自:脚本之家 这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Pyth ...
- python 列表排序方法sort、sorted技巧篇
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- python sort、sorted高级排序技巧
文章转载自:脚本之家 Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的 ...
随机推荐
- 动态计算area位置
window.onresize = adjuest; function adjuest(){ var picw = $(".imgbox img").width(); var pi ...
- 洛谷P4360 [CEOI2004]锯木厂选址(dp 斜率优化)
题意 题目链接 Sol 枚举第二个球放的位置,用前缀和推一波之后发现可以斜率优化 // luogu-judger-enable-o2 #include<bits/stdc++.h> #de ...
- DOM增删操作(创建删除表格)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- web百度地图离线开发
公司现在做的一个项目本来用的是google离线地图,但是发现谷歌的地图数据很久没更新数据了,中国的城市发展这么快,好多地方地图和现实都对不上了. 发现百度地图数据更新挺快的(呵呵,毕竟是国产的吗),最 ...
- Flex Box 简单弹性布局
弹性盒子模型有两种规范:早起的display:box 和后期的display:flex.它可以轻易的实现均分.浮动.居中等灵活布局,在移动端只考虑webkit内核时很实用. 一.display:box ...
- mysql 日期时间类型
datetime timestamp year date time drop table test;create table test (dt datetime, ts timestamp, y ye ...
- node和iisnode express手工安装
一.安装node.js的x86版本: 这样,node.js会安装在C:\Program Files (x86)\nodejs,这符合iisnode express7版本的期待. 二.安装iisnode ...
- 团队项目——软件需求分析(NABCD)
一.团队项目简介 团队名称:SmartCoder 项目名称:<一起> 二.针对 " 地图可视化查看发布的内容 " 这一特点进行 NABCD 分析 N(Need需求) 往 ...
- leetCode题解之寻找插入位置
1.问题描述 Search Insert Position Given a sorted array and a target value, return the index if the targe ...
- [VS2008] 安装 AnkhSVN 后,选项中选择它,Pending Changes 窗口一闪而过,源代码管理工具自动跳回 【None】
执行以下命令以修复: "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" /re ...