Python sorted对list和dict排序

所属分类: 脚本专栏 / python 阅读数: 1756
收藏 0 赞 0 分享

sorted语法

sorted(iterable, key=None, reverse=False)

参数说明:

 - iterable -- 可迭代对象。
 - key --主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
 - reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。

返回:
 - 一个新list对象 

sorted对字典dict排序

①按键key排序

from operator import itemgetter
dict = {3: 'B', 1: 'A', 2: 'C'}

# 按key升序  .items()取得3个(key,value)
# lambda x: x[0]取(key,value)的key  即(3,1,2)
d1 = sorted(dict.items(), key=lambda x: x[0], reverse=False) # <class 'list'>

# 按key降序  itemgetter类似lambda
d2 = sorted(dict.items(), key=itemgetter(0), reverse=True) # <class 'list'>

# 输出
print(d1, type(d1)) # [(1, 'A'), (2, 'C'), (3, 'B')] <class 'list'>
print(d2, type(d2)) # [(3, 'B'), (2, 'C'), (1, 'A')] <class 'list'>

[(1, ‘A'), (2, ‘C'), (3, ‘B')] <class ‘list'>
[(3, ‘B'), (2, ‘C'), (1, ‘A')] <class ‘list'>

②按值value排序

from operator import itemgetter
dict = {3: 'B', 1: 'A', 2: 'C'}

# 按value升序  .items()取得3个(key,value)
# lambda x: x[1]取(key,value)的value  即('B','A','C')
d3 = sorted(dict.items(), key=lambda x: x[1], reverse=False) # <class 'list'>

# 按value降序  itemgetter类似lambda
d4 = sorted(dict.items(), key=itemgetter(1), reverse=True) # <class 'list'>

print(d3, type(d3)) # [(1, 'A'), (3, 'B'), (2, 'C')] <class 'list'>
print(d4, type(d4)) # [(2, 'C'), (3, 'B'), (1, 'A')] <class 'list'>

[(1, ‘A'), (3, ‘B'), (2, ‘C')] <class ‘list'>
[(2, ‘C'), (3, ‘B'), (1, ‘A')] <class ‘list'>

sorted排序list

①按一种规则排序list

from operator import itemgetter
data = [('c', 3, 'Apple'), ('d', 1, 'Cat'), ('a', 2, 'Banana')]
# 根据字母升序
print(sorted(data, key=lambda x: x[0], reverse=False)) # <class 'list'>
# 根据数字升序
print(sorted(data, key=lambda x: x[1], reverse=False)) # <class 'list'>
# 根据单词升序
print(sorted(data, key=lambda x: x[2], reverse=False)) # <class 'list'>

[('a', 2, 'Banana'), ('c', 3, 'Apple'), ('d', 1, 'Cat')]
[('d', 1, 'Cat'), ('a', 2, 'Banana'), ('c', 3, 'Apple')]
[('c', 3, 'Apple'), ('a', 2, 'Banana'), ('d', 1, 'Cat')]

②按多种规则排序list

# 先按照成绩降序排序,相同成绩的按照名字升序排序:
d1 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}]
l = sorted(d1, key=lambda x:(-x['score'], x['name']))
print(l)

[{'name': 'alice', 'score': 38}, {'name': 'christ', 'score': 28}, {'name': 'darl', 'score': 28}, {'name': 'bob', 'score': 18}]

sorted排序list和dict的混合

 先看看我们排序的有哪些类型的数据结构

#### 二维list排序
l1 = [['Bob', 95.00, 'A'], ['Alan', 86.0, 'C'], ['Mandy', 82.5, 'A'], ['Rob', 86, 'E']]

#### list中混合字典
l2 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}]

#### 字典中混合list
d1 = {'Li': ['M', 7], 'Zhang': ['E', 2], 'Wang': ['P', 3], 'Du': ['C', 2], 'Ma': ['C', 9], 'Zhe': ['H', 7]}

#### 对字典中的多维list进行排序
d2 = {
  'Apple': [['44', 88], ['11', 33], ['22', 88]],
  'Banana': [['55', 43], ['11', 68], ['44', 22]],
  'Orange':[['22', 22], ['55', 41], ['44', 42], ['33', 22]]
}

二维list排序

from operator import itemgetter
l1 = [['Bob', 95.00, 'A'], ['Alan', 86.0, 'C'], ['Mandy', 82.5, 'A'], ['Rob', 86, 'E']]
# 按先按成绩号升序,再按成绩数值升序
print(sorted(l1, key=itemgetter(2, 1), reverse=False))
# 按先按成绩号升序,再按成绩数值降序序
print(sorted(l1, key=lambda x:(x[2], -x[1]), reverse=False))

[[‘Mandy', 82.5, ‘A'], [‘Bob', 95.0, ‘A'], [‘Alan', 86.0, ‘C'], [‘Rob', 86, ‘E']]
[[‘Bob', 95.0, ‘A'], [‘Mandy', 82.5, ‘A'], [‘Alan', 86.0, ‘C'], [‘Rob', 86, ‘E']]

2. list中混合字典

from operator import itemgetter
# 先按照成绩降序排序,相同成绩的按照名字升序排序:
l2 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}]
print(sorted(l2, key=lambda x:(-x['score'], x['name'])))
print(sorted(l2, key=itemgetter('score', 'name')))

[{‘name': ‘alice', ‘score': 38}, {‘name': ‘christ', ‘score': 28}, {‘name': ‘darl', ‘score': 28}, {‘name': ‘bob', ‘score': 18}]
[{‘name': ‘bob', ‘score': 18}, {‘name': ‘christ', ‘score': 28}, {‘name': ‘darl', ‘score': 28}, {‘name': ‘alice', ‘score': 38}]

3. 字典中混合list

d1 = {'Li': ['M', 7], 'Zhang': ['E', 2], 'Wang': ['P', 3], 'Du': ['C', 2], 'Ma': ['C', 9], 'Zhe': ['H', 7]}
# sort返回的是list,如果需要转为dict,再sorted前面套一个dict()就可以了
print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) ))) # 对字符比较需要ord。如果是'123'字符串数字可以使用int。
# print(sorted(d1.items(), key=lambda x:(x[1][1], -ord(x[1][0]) )))

[(‘Zhang', [‘E', 2]), (‘Du', [‘C', 2]), (‘Wang', [‘P', 3]), (‘Li', [‘M', 7]), (‘Zhe', [‘H', 7]), (‘Ma', [‘C', 9])]

4. 对字典中的多维list进行排序

d2 = {
  'Apple': [['44', 88], ['11', 33], ['22', 88]],
  'Banana': [['55', 43], ['11', 68], ['44', 22]],
  'Orange':[['22', 22], ['55', 41], ['44', 42], ['33', 22]]
}
for key, value in d2.items():
  d2[key] = sorted(value, key=lambda x:(x[1], -int(x[0]))) # 按list第二列升序,相同则按第一列降序,参考二维list排序
print(d2)

{‘Apple': [[‘11', 33], [‘44', 88], [‘22', 88]], ‘Banana': [[‘44', 22], [‘55', 43], [‘11', 68]], ‘Orange': [[‘33', 22], [‘22', 22], [‘52', 41], [‘44', 42]]}

更多精彩内容其他人还在看

python打印直角三角形与等腰三角形实例代码

这篇文章主要给大家介绍了关于python打印直角三角形与等腰三角形的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

基于python解线性矩阵方程(numpy中的matrix类)

这篇文章主要介绍了基于python解线性矩阵方程(numpy中的matrix类),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

vim自动补全插件YouCompleteMe(YCM)安装过程解析

这篇文章主要介绍了vim自动补全插件YouCompleteMe(YCM)安装过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中@property和property函数常见使用方法示例

这篇文章主要介绍了python中@property和property函数常见使用方法,结合实例形式分析了Python @property和property函数功能、使用方法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python中bytes和str类型的区别

这篇文章主要介绍了python中bytes和str类型的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解python中*号的用法

这篇文章主要介绍了python中*号的用法,文中通过代码给大家介绍了双星号(**)的用法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python超时重新请求解决方案

这篇文章主要介绍了python超时重新请求解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python多继承(钻石继承)问题和解决方法简单示例

这篇文章主要介绍了python多继承(钻石继承)问题和解决方法,结合实例形式分析了Python多继承调用父类初始化方法相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Python解密仿射密码

这篇文章主要介绍了基于Python解密仿射密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Python实现船舶的MMSI的获取(推荐)

工作中遇到一个需求,需要通过网站查询船舶名称得到MMSI码,网站来自船讯网。这篇文章主要介绍了基于Python实现船舶的MMSI的获取,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多