首页 > 编程语言 >Python 中的 sorted 和 sort的区别

Python 中的 sorted 和 sort的区别

时间:2022-09-06 12:23:47浏览次数:55  
标签:sort name Python gpa 姓名 sorted 成绩 科目


Python 中的 sorted 和 sort的区别
#sort 与 sorted 区别:
# sorted() 是内置函数. sorted 可以对所有可迭代的对象进行排序操作,有返回值,返回列表;
# sort 是list 上的方法,是对已经存在的列表进行inplace的操作,无返回值。
list1 = [11,5,33,9,222,6,3,8,7,99,123,24,456,6878,242,22,666,4654,87]
list1.sort(reverse=False)
list2 = sorted(list1,reverse=False)
# 上面两条命令的结果相等
# 列表本身可以执行三元表达式
list1 = [11,5,33,9,222,6,3,8,7,99,123,24,456,6878,242,22,666,4654,87]
list1 = [x for x in list1 if x>99]
print(list1) # [222, 123, 456, 6878, 242, 666, 4654]
# 倒序排列
list1.sort(reverse= True)
print(list1) # [6878, 4654, 666, 456, 242, 222, 123]

# 当列表包含子列表\元组或子字典时,sort方法的key的作用
# 单独的字典没有sort方法
# sort
list2 = [('age',18), ('age',20),('age', 15)]
list2.sort(key = lambda x :x[1], reverse=False)
print(list2) # [('age', 15), ('age', 18), ('age', 20)]
# sorted
list2 = [('age',18), ('age',20),('age', 15)]
list2 = sorted(list2)
print(list2) # [('age', 15), ('age', 18), ('age', 20)]
list2 = sorted(list2,reverse=True)
print(list2) # [('age', 20), ('age', 18), ('age', 15)]

# sort
list3 = [[11,2], [3,4], [61,1], [6,2]]
list3.sort(key = lambda x:x[0],reverse=True)
print(list3) # [[61, 1], [11, 2], [6, 2], [3, 4]]
list4 = list(filter(lambda x:x[0]>5, list3)) # sort方法配合filter
print(list4) # [[61, 1], [11, 2], [6, 2]]
# sorted
list3 = sorted(list3,key = lambda x :x[0],reverse=True)
print(list3) # [[61, 1], [11, 2], [6, 2], [3, 4]]
list4 = list(filter(lambda x : x[0]>10, list3))
print(list4) # [[61, 1], [11, 2]]

# sort
list5 = [{'name':'lisa', 'gpa':19},
{'name':'lili', 'gpa':88},
{'name':'tom', 'gpa':81},
{'name':'jack', 'gpa':66}
]

list5.sort(key = lambda x:x['gpa'],reverse= True)
print(list5) # [{'name': 'lili', 'gpa': 88}, {'name': 'tom', 'gpa': 81}, {'name': 'jack', 'gpa': 66}, {'name': 'lisa', 'gpa': 19}]

alldate = [{'学号': 2.0, '姓名': '李四', '科目': '数学', '成绩': 77.0},
{'学号': 3.0, '姓名': '王五', '科目': '数学', '成绩': 78.0},
{'学号': 1.0, '姓名': '张三', '科目': '数学', '成绩': 90.0},
{'学号': 3.0, '姓名': '王五', '科目': '英语', '成绩': 76.0},
{'学号': 1.0, '姓名': '张三', '科目': '英语', '成绩': 90.0},
{'学号': 2.0, '姓名': '李四', '科目': '英语', '成绩': 91.0},
{'学号': 1.0, '姓名': '张三', '科目': '语文', '成绩': 88.0},
{'学号': 3.0, '姓名': '王五', '科目': '语文', '成绩': 88.0},
{'学号': 2.0, '姓名': '李四', '科目': '语文', '成绩': 89.0}]

alldate.sort(key=lambda x :(x['科目'],x['成绩']))
print(alldate) # [{'学号': 2.0, '姓名': '李四', '科目': '数学', '成绩': 77.0}, {'学号': 3.0, '姓名': '王五', '科目': '数学', '成绩': 78.0}, {'学号': 1.0, '姓名': '张三', '科目': '数学', '成绩': 90.0}, {'学号': 3.0, '姓名': '王五', '科目': '英语', '成绩': 76.0}, {'学号': 1.0, '姓名': '张三', '科目': '英语', '成绩': 90.0}, {'学号': 2.0, '姓名': '李四', '科目': '英语', '成绩': 91.0}, {'学号': 1.0, '姓名': '张三', '科目': '语文', '成绩': 88.0}, {'学号': 3.0, '姓名': '王五', '科目': '语文', '成绩': 88.0}, {'学号': 2.0, '姓名': '李四', '科目': '语文', '成绩': 89.0}]

# sorted

list5 = [{'name':'lisa', 'gpa':19},
{'name':'lili', 'gpa':88},
{'name':'tom', 'gpa':81},
{'name':'ajack', 'gpa':66}
]

list6 = sorted(list5, key=lambda x:(x['gpa'], x['name']))
print(list6) # [{'name': 'lisa', 'gpa': 19}, {'name': 'ajack', 'gpa': 66}, {'name': 'tom', 'gpa': 81}, {'name': 'lili', 'gpa': 88}]

alldate = [{'学号': 2.0, '姓名': '李四', '科目': '数学', '成绩': 77.0},
{'学号': 3.0, '姓名': '王五', '科目': '数学', '成绩': 78.0},
{'学号': 1.0, '姓名': '张三', '科目': '数学', '成绩': 90.0},
{'学号': 3.0, '姓名': '王五', '科目': '英语', '成绩': 76.0},
{'学号': 1.0, '姓名': '张三', '科目': '英语', '成绩': 90.0},
{'学号': 2.0, '姓名': '李四', '科目': '英语', '成绩': 91.0},
{'学号': 1.0, '姓名': '张三', '科目': '语文', '成绩': 88.0},
{'学号': 3.0, '姓名': '王五', '科目': '语文', '成绩': 88.0},
{'学号': 2.0, '姓名': '李四', '科目': '语文', '成绩': 89.0}]

alldate = sorted(alldate, key=lambda x:(x['姓名'],x['科目']),reverse=True)
print(alldate) # [{'学号': 3.0, '姓名': '王五', '科目': '语文', '成绩': 88.0}, {'学号': 3.0, '姓名': '王五', '科目': '英语', '成绩': 76.0}, {'学号': 3.0, '姓名': '王五', '科目': '数学', '成绩': 78.0}, {'学号': 2.0, '姓名': '李四', '科目': '语文', '成绩': 89.0}, {'学号': 2.0, '姓名': '李四', '科目': '英语', '成绩': 91.0}, {'学号': 2.0, '姓名': '李四', '科目': '数学', '成绩': 77.0}, {'学号': 1.0, '姓名': '张三', '科目': '语文', '成绩': 88.0}, {'学号': 1.0, '姓名': '张三', '科目': '英语', '成绩': 90.0}, {'学号': 1.0, '姓名': '张三', '科目': '数学', '成绩': 90.0}]

# 我的总结
# sort是本身自带方法,不能改变自身,只有与filter、map、三元表达式才能生成新的数据
# sorted是内置函数,并且有返回值,也可以与filter、map配合生成新的数据。

标签:sort,name,Python,gpa,姓名,sorted,成绩,科目
From: https://www.cnblogs.com/leeyong49/p/16661328.html

相关文章

  • 【python】sort 排序
    sort排序fromoperatorimportitemgettera=[ {'name':'小张','create_time':'2020-10-1609:56'}, {'name':'小王','create_time':'2020-10-1609:57'}, {'name'......
  • 晓晓---python文件的读写模式的理解
    1.python读取文件模式的自我理解:'r'openforreading(default)----只读模式打开文件,不能写;'w'openforwriting,truncatingthefilefirst----只写模式......
  • Python-注解-类型注解
    类型注解的作用Python是动态语言,其显著特点是在声明变量时,你不需要显式声明它的类型。程序运行时会推断出变量age是int类型但是:如果你代码某些变量的类型有错,编辑器......
  • 复习python基础
    ......
  • python中的map函数
    python中的map函数1map()函数的简介以及语法:map是python内置函数,会根据提供的函数对指定的序列做映射。map()函数的格式是:map(function,iterable,...)第一个参数接受一......
  • 用于数据库/消息队列的 Python 传输
    用于数据库/消息队列的Python传输很多时候,在对项目进行编码时,我们希望数据库或其他组件的代码与代码库的其余部分分开。传输帮助我们隔离组件的整个代码,以便当我们需要......
  • Python 装饰器
    Python装饰器假设我们想为已经编写的代码添加额外的功能,例如我们想在一个特殊的trycatch块中捕获任何错误,我们可以按如下方式进行。defsome_decorator(func):d......
  • 2022 年最适合游戏开发者使用的 10 个 Python 框架
    2022年最适合游戏开发者使用的10个Python框架供游戏开发人员立即使用的Python框架随着游戏编程语言的集成,游戏业务在最新的技术市场中蓬勃发展。在游戏创作者的编......
  • 解决python使用过程出现的问题
    关于报错“AttributeError:partiallyinitializedmodule‘requests‘hasnoattribute‘get‘的解决方法报错:AttributeError:partiallyinitializedmodule‘requests......
  • python3下载及安装教程(Windows)
    Python目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,一般默认装的是Python2版本,Windows系统上没有Pyhton环境,需要我们手动安装一下,现在一般都是python3......