赞
踩
Python中有内置函数sorted(), list(列表)中也有函数list.sort()都可以进行排序。
list.sort():列表的排序方法,会改变原来列表的的排序、只适用于列表排序、所以效率高
sorted():函数,适用于任意可迭代对象(字符串、元祖、列表、字典等),返回的是一个新列表。
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
-- iterable:可迭代类型,例如字典、列表、
-- cmp:比较函数
-- key:可迭代类型中某个属性,对给定元素的每一项进行排序
-- reverse:降序或升序--返回值:返回新的已排序的列表
- ls = [3, 6, 1, 8, 2, 7, 9]
- print(sorted(ls))
- # [1, 2, 3, 6, 7, 8, 9]
(1)给key参数传入了一个lambda函数表达式,其person就代表列表里的每一个元素
ls = [('john', 178, 18), ('jane', 175, 23), ('dave', 180, 20),] print(sorted(ls, key=lambda person : person[2])) # sort by age # [('john', 178, 18), ('dave', 180, 20), ('jane', 175, 23)]
dic = {"a":"2","b":"8","c":"6"} print(sorted(dic.items(),key=lambda x:x[0],reverse=True)) # 按key降序排序 # [('c', '6'), ('b', '8'), ('a', '2')] # 返回的是列表
dic = {"a":"2","b":"8","c":"6"} print(sorted(dic.items(),key=lambda x:x[1],reverse=True)) # 按value降序排序 # [('b', '8'), ('c', '6'), ('a', '2')] # 返回的是列表
(2)给key参数传operator.itemgetter方法
import operator ls = [{"name":"Jack","age":18},{"name":"Lily","age":28}] ## 按照age排序 print(sorted(ls, key=operator.itemgetter('age'), reverse=True)) # [{'name': 'Lily', 'age': 28}, {'name': 'Jack', 'age': 18}]
import operator ls = [ {"name":"Jack","age":18, 'height':180}, {"name":"Lily","age":28, 'height':175}, {"name":"Jerry","age":28, 'height':173} ] ## 先按照age排序,若age相同,再比较height print(sorted(ls, key=operator.itemgetter('age','height'))) """ [ {'name': 'Jack', 'age': 18, 'height': 180}, {'name': 'Jerry', 'age': 28, 'height': 173}, {'name': 'Lily', 'age': 28, 'height': 175} ] """
import operator ## (name, scores, age) students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)] ## 先按照成绩排序,成绩相同再比较年龄 print(sorted(students, key=operator.itemgetter(1,2))) # [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
使用lambda表达式:
dic = [{"name":"Jack","age":18},{"name":"Lily","age":28}] print(sorted(dic,key=lambda x:x['age'],reverse=True)) # [{'name': 'Lily', 'age': 28}, {'name': 'Jack', 'age': 18}]使用operator方式:
import operator dic = [{"name":"Jack","age":18},{"name":"Lily","age":28}] print(sorted(dic, key=operator.itemgetter('age'),reverse=True)) # [{'name': 'Lily', 'age': 28}, {'name': 'Jack', 'age': 18}]
思路是将字典转换为元组或列表
dic = {1001:{"aa":1,"bb":2},1002:{"aa":2,"bb":0},1003:{"aa":0,"bb":3}} sorted_list = sorted(dic.items(),key=lambda x:x[1]['bb']) # x为(1001, {'aa': 1, 'bb': 2})的元组形式,按bb键排序 print(sorted_list) # [(1002, {'aa': 2, 'bb': 0}), (1001, {'aa': 1, 'bb': 2}), (1003, {'aa': 0, 'bb': 3})] sorted_dic = {i[0]:i[1] for i in sorted_list} print(sorted_dic) # {1002: {'aa': 2, 'bb': 0}, 1001: {'aa': 1, 'bb': 2}, 1003: {'aa': 0, 'bb': 3}} ########################################################################################## ## 补充知识: print(list(dic.items())) # [(1001, {'aa': 1, 'bb': 2}), (1002, {'aa': 2, 'bb': 0}), (1003, {'aa': 0, 'bb': 3})] """ dic.items()会将dic字典中的每项键值对key:value存储为一个元组(key,value),如下: dict_items([(1001, {'aa': 1, 'bb': 2}), (1002, {'aa': 2, 'bb': 0}), (1003, {'aa': 0, 'bb': 3})]) list(dic.items())可以将其转换为列表,即[(1001, {'aa': 1, 'bb': 2}), (1002, {'aa': 2, 'bb': 0}), (1003, {'aa': 0, 'bb': 3})] """核心是:sorted_list = sorted(dic.items(),key=lambda x:x[1]['bb']) # x为(1001, {'aa': 1, 'bb': 2})的元组形式,按bb键排序。
dic.items()会将dic字典中的每项键值对key:value存储为一个元组(key,value),如下:
dict_items([(1001, {'aa': 1, 'bb': 2}), (1002, {'aa': 2, 'bb': 0}), (1003, {'aa': 0, 'bb': 3})])
list(dic.items())可以将其转换为列表,即[(1001, {'aa': 1, 'bb': 2}), (1002, {'aa': 2, 'bb': 0}), (1003, {'aa': 0, 'bb': 3})]
思路参考3.2,可以先转换为其他方便排序的类型,排完序再转回原来的形式。
可以将列表中的字典先放入到一个字典中,再对整个字典进行排序,在排序完成后,再转换成原来的列表套字典形式:
import operator # 将成绩升序排序{name:scores} ls = [{"min":89},{"fei":90},{"hao":84},{"jhon":98}] dic = {key:value for x in ls for key,value in x.items()} print(dic) # {'min': 89, 'fei': 90, 'hao': 84, 'jhon': 98} sorted_list = sorted(dic.items(),key=operator.itemgetter(1)) print(sorted_list) # [('hao', 84), ('min', 89), ('fei', 90), ('jhon', 98)] sorted_ls = [{i[0]:i[1]} for i in sorted_list] print(sorted_ls) # [{'hao': 84}, {'min': 89}, {'fei': 90}, {'jhon': 98}]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。