一、Python的排序

1、reversed()

这个很好理解,reversed英文意思就是:adj. 颠倒的;相反的;(判决等)撤销的

print list(reversed(['dream','a','have','I']))
#['I', 'have', 'a', 'dream']

2、让人糊涂的sort()与sorted()

在Python 中sorted是内建函数(BIF),而sort()是列表类型的内建函数list.sort()。

sorted()

sorted(iterable[, cmp[, key[, reverse]]])

Return a new sorted list from the items in iterable.

The optional arguments(可选参数) cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types).

cmp specifies(指定) a custom comparison function of two arguments (iterable(可迭代的) elements) which should return a negative(复数), zero or positive(正数) number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

#字符串排序使用是字典序,而非字母序
"""sorted()按照字典序排序"""
lis = ['a','c','z','E','T','C','b','A','Good','Tack']
print sorted(lis) #['A', 'C', 'E', 'Good', 'T', 'Tack', 'a', 'b', 'c', 'z']

关于字典序:

可参考百度百科。http://baike.baidu.com/view/4670107.htm

根据ASCII排,具体如下:
0-9(对应数值48-59);
A-Z(对应数值65-90);
a-z(对应数值97-122);

------------

标准序: 短在前,长在后,等长的依次比字母,
如to < up < cap < cat < too < two <boat < boot < card
字典序: 依次比字母,
如boat < boot <cap < card < cat < to < too< two < up

更有甚者说字典序就是字典的排序,像字典一样。我一直没有找到权威的说明,什么是字典序????求答案!!

sort()

s.sort([cmp[, key[, reverse]]])

三、Python的字典排序

1、关于Python字典的一些特征

无序:

字典,形如 dic = {'a':1 , 'b':2 , 'c': 3},字典中的元素没有顺序,所以dic[0]是有语法错误的。

无重:

不可以有重复的键值,所以 dic.add['c'] = 4后,字典变成 {'a':1 , 'b':2 , 'c': 4}.

2、根据“键”或“键值”进行不同顺序的排序

函数原型:sorted(dic,value,reverse)

解释:dic为比较函数,value 为排序的对象(这里指键或键值),

reverse:注明升序还是降序,True--降序,False--升序(默认)

3、例子:

dic = {'a':31, 'bc':5, 'c':3, 'asd':4, '33':56, 'd':0}
想把dic的value按照从大到小排序(value都是整数)。

dic = {'a':31, 'bc':5, 'c':3, 'asd':4, '33':56, 'd':0}
print sorted(dic.iteritems(), key=lambda d:d[1], reverse = False )
#[('d', 0), ('c', 3), ('asd', 4), ('bc', 5), ('a', 31), ('33', 56)]

解释如下:

(1)、dic.iteritems(),返回字典键值对的元祖集合

print dic.iteritems()   #<dictionary-itemiterator object at 0x00B71A80>

for obj in dic.iteritems():
print obj,obj[0],obj[1] #('a', 31) a 31
#('c', 3) c 3
#('d', 0) d 0
#('bc', 5) bc 5
#('33', 56) 33 56
#('asd', 4) asd 4

(2)、关于排序对象

上述已经说过,value(或key)为排序的对象(这里指键或键值),然而为什么使用lambda函数呢,这里请参阅:点击阅读

key=lambda d:d[1] 是将键值(value)作为排序对象。

key = lambda d:d[1]
for i in dic.iteritems():
print key(i), #输出31 3 0 5 56 4,这些都是字典dic的值

如果选择 key = lambda d:d[0],则选择【键Key】作为排序对象。

(3)、reverse

reverse 是否反向,reverse=Ture表示反向。

(4)、注意:

sorted(dic.iteritems(), key=lambda d:d[1], reverse = False )将每一项dic.iteritems()键值对的元祖进行迭代,每一项都作为参数传入key()函数(我说的是这个:key=lambda d:d[1],)中。

 4、回顾

lis = ['a','bc','c','asd','33','d']
print sorted(lis) #['33', 'a', 'asd', 'bc', 'c', 'd']
依次比字母, 如boat < boot <cap < card < cat < to < too< two < up

5.问题

具体实例可参考:[**python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例**](http://wangwei007.blog.51cto.com/68019/1100742)

现在有这种情况,排序中排序。如大题号排序,然后大题对应的小题号也排序,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
lis = [{'Big':3'small':2},{'Big':3'small':4},{'Big':2'small':2}, {'Big':3'small':1},{'Big':2'small':1},{'Big':1'small':1}]
 
# 大题号排序
li = sorted(lis, key=lambda s: s['Big'])
 
# 输出:
#[{'small': 1, 'Big': 1}, {'small': 2, 'Big': 2}, {'small': 1, 'Big': 2}, {'small': 2, 'Big': 3}, {'s
mall': 4, 'Big': 3}, {'small': 1, 'Big': 3}]
 
# 小题号排序:
sort_ff = []
no = set([i['Big'for in li])
for obj in no:
li_ = []
for in ff:
if i['Big'== obj:
li_.append(i)
= sorted(li_, key=lambda s: s['small'])
for in l:
sort_ff.append(j)
 
# 输出结果:
[{'small'1'Big'1}, {'small'1'Big'2}, {'small'2'Big'2}, {'small'1'Big'3}, {'small'2'Big'3}, {'small'4'Big'3}]

善用sort() 或 sorted(), a.sort() 已改变其结构,b = a.sort() 是错误的写法! 而 sorted(a, ...)并没有改变a的结构。

python中字典排序的更多相关文章

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

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

  2. 深入Python(1): 字典排序 关于sort()、reversed()、sorted()

    http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...

  3. python中字典的循环遍历的两种方式

    开发中经常会用到对于字典.列表等数据的循环遍历,但是python中对于字典的遍历对于很多初学者来讲非常陌生,今天就来讲一下python中字典的循环遍历的两种方式. 注意: python2和python ...

  4. Python中字典和集合

    Python中字典和集合 映射类型: 表示一个任意对象的集合,且可以通过另一个几乎是任意键值的集合进行索引 与序列不同,映射是无序的,通过键进行索引 任何不可变对象都可用作字典的键,如字符串.数字.元 ...

  5. python中字典按键、值进行排序

    看到排序,就不禁想起python中的sort和sorted sort是列表中的方法,用于对列表进行排序(改变的是原列表,不返回新列表) 用法: list.sort(key=None,reverse=T ...

  6. python中字典以key排序,以value排序。以及通过value找key的方式

    1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...

  7. python中字典的使用

    python中的字典的特性: 在字典中的元素是散列存放的,没有顺序, 在进行增删改查的时候使用字典中需要的关键字(key)即可. 一: 创建字典 1)直接定义一个: dict = {'ob1':'co ...

  8. python中字典dict的操作

    字典可存储任意类型的对象,由键和值(key - value)组成.字典也叫关联数组或哈希表. dict = {' , 'C' : [1 , 2 , 3] } dict['A'] = 007 # 修改字 ...

  9. python接口自动化(九)--python中字典和json的区别(详解)

    简介 这篇文章的由来是由于上一篇发送post请求的接口时候,参数传字典(dict)和json的缘故,因为python中,json和dict非常类似,都是key-value的形式,为啥还要这么传参,在群 ...

随机推荐

  1. 关于linux redhat及免费的问题

    我想下载一个redhat的免费版,这个免费版是不是就不叫redhat了? 是Fedora吗? 不要推荐 红旗 以及其它的版本了,谢谢!!! 007struggle | 浏览 12141 次 发布于20 ...

  2. Linux操作系统中对于NTFS读取目录功能的实现

    1: /* 2: * We use the same basic approach as the old NTFS driver, i.e. we parse the 3: * index root ...

  3. 广度优先搜索(Breadth First Search)

    Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...

  4. zip压缩详细分析

    该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...

  5. leetcode.哈希表.594最长和谐子序列-Java

    1. 具体题目: 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1.现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例 1: 输入: [1,3,2,2,5 ...

  6. <思维导图>思维导图

  7. SpringCloud广告系统随想

    广告系统需要两个方面:一个广告投放系统,一个是广告检索系统. 两个系统针对的人群不同: 1.广告投放系统针对的是广告主,他们出资进行广告的投放,将广告的一些数据投放到系统中. 2.广告检索系统针对的是 ...

  8. docker学习---搭建Docker LAMP环境

    1.环境 系统版本:CentOS Linux release 7.4.1708 docker版本:docker-ce-18.09 主机IP:192.168.121.121 2.载入MySQL和PHP镜 ...

  9. Go学习笔记:初识Go语言

    Go语言简介 Go语言是Google(谷歌)公司开发的一款静态型.编译型并自带垃圾回收机制和并发的编程语言. Go语言的风格类似于C语言.其语法在C语言的基础上进行了大幅的简化,去掉了不需要的表达式括 ...

  10. kubeadm生成的token重新获取

    当你的token忘了或者过期,解决办法如下: 1.先获取token #如果过期可先执行此命令kubeadm token create #重新生成token#列出tokenkubeadm token l ...