python的排序的函数,python排序函数指令

sorted函数python

sorted函数python介绍如下

专注于为中小企业提供成都做网站、网站建设、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业米林免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

sorted() 作为 Python 内置函数之一,其功能是对序列(列表、元组、字典、集合、还包括字符串)进行排序。

sorted() 函数的基本语法格式如下

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

其中,iterable 表示指定的序列,key 参数可以自定义排序规则;reverse 参数指定以升序(False,默认)还是降序(True)进行排序。sorted() 函数会返回一个排好序的列表。

注意,key 参数和 reverse 参数是可选参数,即可以使用,也可以忽略。

演示sorted()函数的基本代码用法:

#对列表进行排序

a = [5,3,4,2,1]

print(sorted(a))

#对元组进行排序

a = (5,4,3,1,2)

print(sorted(a))

#字典默认按照key进行排序

a = {4:1,\

5:2,\

3:3,\

2:6,\

1:8}

print(sorted(a.items()))

#对集合进行排序

a = {1,5,3,2,4}

print(sorted(a))

#对字符串进行排序

a = "51423"

print(sorted(a))

python中对组合数据类型x进行排序的内置函数是

python有两个内置的函数用于实现排序,一个是list.sort()函数,一个是sorted()函数。

区别1:list.sort()函数只能处理list类型数据的排序;sorted()则可以处理多种类型数据的排序。

区别2:list.sort()会修改原来的list为排序后的结果;sorted()不会修改原来的数据,只是返回一个排序后的对象。

下面来动手写点代码进行测试:

list进行简单的排序

1 a=['a','f','c','x','e']2 a.sort()3 a4 [a','c','e','f','x']

sorted()进行简单排序

1 a=['a','f','c','x','e']2 a_new=sorted(a)3 a_new4 ['a', 'c', 'e', 'f', 'x']

list.sort()和sorted都有默认的参数reverse,该参数的默认值为“False”,即默认升序排列。如果要进行降序排列:

1 a=['a','f','c','x','e']2 a_new=sorted(a,reverse=True)3 a_new4 ['x', 'f', 'e', 'c', 'a']

对元组进行排列,不能使用list.sort()函数:

1 a=('a','f','c','x','e')2 a_new=a.sort()3

4 ttributeError: 'tuple' object has no attribute 'sort'

对字典的排序可以按照字典的key或者value进行排序:

1 dic={"aa":11,"ff":5,"ee":22}2 printsorted(dic.keys())3 ['aa', 'ee', 'ff']

从python2.4开始list.sort()和sorted()都引入了key参数来指定一个函数进行排序,有了key参数以后我们就可以对更加复杂的数据进行排序 。

假如需要排序的数据为一个list,但list的每一个元素都是一个字典,排序的方法如下:

dic=[{"name":"sunny","age":20},

{"name":"bell","age":5},

{"name":"jim","age":1},

{"name":"jan","age":10}]print sorted(dic,key=lambda arg:arg.get('age'),reverse=False) #按照每个字典的age的值进行排序,字典的方法dict.get(key)将返回指定键的值。

#输出结果为:[{'age': 1, 'name': 'jim'}, {'age': 5, 'name': 'bell'}, {'age': 10, 'name': 'jan'}, {'age': 20, 'name': 'sunny'}]

print sorted(dic,key=lambda arg:arg.get('name'),reverse=False) #按照每个字典的name的值进行排序

#输出结果为:[{'age': 5, 'name': 'bell'}, {'age': 10, 'name': 'jan'}, {'age': 1, 'name': 'jim'}, {'age': 20, 'name': 'sunny'}]

假如需要排序的数据为一个二维的list,即list的每一个元素也是一个list,方法与上面的例子类似:

a=[['100','8','30'],['200','5','50'],['300','1','20']]print sorted(a,key=lambda arg:arg[1]) #按照每个list的第1个元素进行排序

[['300', '1', '20'], ['200', '5', '50'], ['100', '8', '30']]

前面的2个例子中,key参数的值都是函数。在sorted函数中,key参数后面的函数的参数的数量只能是一个。lambda arg:arg.get('age')是一个lambda匿名函数,它是一个没有函数名的单语句函数。冒号左边是参数,冒号右边的返回值,返回值将被用于排序。

python字母顺序排序

1. (按字母顺序排列)——sort()函数

例:

cars =['bmw','audi','toyota','subaru']

cars.sort()

print(cars)

输出得到

['audi', 'bmw', 'subaru', 'toyota']

请点击输入图片描述

请点击输入图片描述

2.按字母反序排列——reverse函数

cars =['bmw','audi','toyota','subaru']

cars.sort(reverse=True)# reverse“adj. 反面的;颠倒的;n. 倒转,反向”

print(cars)

输出得到

['toyota', 'subaru', 'bmw', 'audi']

请点击输入图片描述

请点击输入图片描述

3.对列表进行临时排序——sorted()函数

cars =['bmw','audi','toyota','subaru']

print("Here is the original list:")

print(cars)

print("\nHere is the sorted list:")

print(sorted(cars))

print("Here is the original list again:")

print(cars)

输出得到

Here is the original list:

['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:

['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:

['bmw', 'audi', 'toyota', 'subaru']

请点击输入图片描述

请点击输入图片描述

4. 倒着打印列表——reverse()函数

cars =['bmw','audi','toyota','subaru']

print(cars)

cars.reverse()

print(cars)

输出得到

['bmw', 'audi', 'toyota', 'subaru']

['subaru', 'toyota', 'audi', 'bmw']

请点击输入图片描述

请点击输入图片描述


当前文章:python的排序的函数,python排序函数指令
转载注明:http://lszwz.com/article/dsciopd.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款