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 sectionMutable 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 isNone (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.items(), key=lambda d:d[1], reverse = False )
#[('d', 0), ('c', 3), ('asd', 4), ('bc', 5), ('a', 31), ('33', 56)]

解释如下:

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

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}]

Python的排序的更多相关文章

  1. python sorted排序

    python sorted排序 Python不仅提供了list.sort()方法来实现列表的排序,而且提供了内建sorted()函数来实现对复杂列表的排序以及按照字典的key和value进行排序. s ...

  2. python 常见排序实例

    使用Python 基础排序算法设计,冒泡排序,插入排序,快速排序... 需求 对一组无序数据进行排序算法设计,要求如下: 输入:[1, 3, 5, 23, 75, 34, 456, 86, 22, 7 ...

  3. python 字典排序 关于sort()、reversed()、sorted()

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

  4. python sorted排序用法详解

    sorted排序 python sorted 排序 1. operator函数在介绍sorted函数之前需要了解一下operator函数. operator函数是python的内置函数,提供了一系列常 ...

  5. Python之排序算法:快速排序与冒泡排序

    Python之排序算法:快速排序与冒泡排序 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7828610.html 入坑(简称IT)这一行也有些年头了,但自老师 ...

  6. python实现排序算法 时间复杂度、稳定性分析 冒泡排序、选择排序、插入排序、希尔排序

    说到排序算法,就不得不提时间复杂度和稳定性! 其实一直对稳定性不是很理解,今天研究python实现排序算法的时候突然有了新的体会,一定要记录下来 稳定性: 稳定性指的是 当排序碰到两个相等数的时候,他 ...

  7. python常见排序算法解析

    python——常见排序算法解析   算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...

  8. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  9. <转>python字典排序 关于sort()、reversed()、sorted()

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

随机推荐

  1. ios7适配一些问题以及64位32位

    ios7适配一些问题(http://www.cocoachina.com/ios/20130703/6526.html) 1.iOS应用如何实现64位的支持 http://www.codeceo.co ...

  2. 键盘控制div移动

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...

  3. 修正ios h5上传图时的图片方向问题

     .ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致  为了用户 ...

  4. 出现 could not open jvm.cfg 的解决办法

    出现该的原因很可能是由于卸载 jdk 不干净而引起的(就算是使用工具也一样),在oracle的官方上也是让我们卸载了重装一次.可是重装真的能解决问题吗?  以下方法经过本人的验证,绝对可行! (一定要 ...

  5. root的方法大体上有以下三种

    root的方法大体上有以下三种一.手机软件安卓版直接root.这种方法不需要电脑的支持,也很安全.安卓版软件有:kingroot,360一键root,一键root大师,Towelroot,支持云roo ...

  6. testng标签运行顺序

    testng的annotations运行顺序为: @BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @AfterMethod @AfterClas ...

  7. 项目解析- JspLibrary - part1

    http://rosspc:8080/JspLibrary/ 1. logon界面解析: JS 验证用户名.密码为空 <form name="form1" method=&q ...

  8. java 异常2

    自定义异常类: import java.util.Scanner; /** * Created by lenovo on 2016/8/12. */ class NoMoneyException ex ...

  9. 20145218 《Java程序设计》第02次实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验名称:Java面向对象程序设计 一.实验内容 初步掌握单元测试和T ...

  10. Linux基础:软件安装(rpm,yum,源代码)

    Software Installation on Linux Linux安装分为rpm包(可通过yum或者是rpm命令安装)和源码包(源代码或者是编译过的二进制码)两种. Linux是开源系统,很多应 ...