Python内置函数
- Python内置函数- __import__() 函数
- Python内置函数- zip()函数
- Python内置函数-vars()函数
- Python内置函数-type()函数
- Python内置函数-tuple 函数
- Python内置函数-super()函数
- Python内置函数-sum()函数
- Python内置函数-str()函数
- Python内置函数-staticmethod()函数
- Python内置函数-sorted()函数
- Python内置函数-slice()函数
- Python内置函数-setattr()函数
- Python内置函数-set() 函数
- Python内置函数-round()函数
- Python内置函数-reversed函数
- Python内置函数-repr()函数
- Python内置函数-range() 函数
- Python内置函数-property() 函数
- Python内置函数-print() 函数
- Python内置函数-pow() 函数
- Python内置函数-ord()函数
- Python内置函数-open()函数
- Python内置函数-oct() 函数
- Python内置函数-next() 函数
- Python内置函数-min() 函数
- Python内置函数-memoryview() 函数
- Python内置函数-max() 函数
- Python内置函数-map()函数
- Python内置函数-locals() 函数
- Python内置函数-list()函数
- Python内置函数-len()函数
- Ptyhon内置函数-iter()函数
- Python内置函数-issubclass()函数
- Python内置函数-isinstance() 函数
- Python内置函数-int() 函数
- Python内置函数-input()函数
- Python内置函数-id()函数
- Python内置函数-hex()函数
- Python内置函数-help()函数
- Python内置函数-hash() 函数
- Python内置函数-hasattr() 函数
- Python内置函数-globals() 函数
- Python内置函数-getattr() 函数
- Python内置函数-frozenset() 函数
- Python内置函数-format()函数
- Python内置函数-float()函数
- Python内置函数-filter()函数
- Python内置函数-exec()函数
- Python内置函数-eval()函数
- Python内置函数-enumerate() 函数
- Python内置函数-divmod()函数
- Python内置函数-dir()函数
- Python内置函数-dict()函数
- Python内置函数- delattr()函数
- Python内置函数-complex()函数
- Python内置函数-compile() 函数
- Python内置函数-classmethod()函数
- Python内置函数-chr() 函数
- Python内置函数-callable() 函数
- Python内置函数-bytes函数
- Python内置函数-bytearray() 函数
- Python内置函数-bool()函数
- Python内置函数-bin() 函数
- Python内置函数-ascii()函数
- Python内置函数-any() 函数
- Python内置函数-all() 函数
- Python内置函数-abs() 函数
- Python内置函数-大全
Python内置函数-sorted()函数
Python3内置函数--sorted()函数
sorted()函数描述:
sorted() 函数对所有可迭代的对象进行排序操作。
sorted()函数语法:
sorted(iterable, key=None, reverse=False)
sorted()函数参数:
iterable -- 可迭代对象。
key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
sorted()函数返回值:
返回重新排序的列表。
sorted()函数实例:
my_list = [3,4,5,1,2,0] my_tuple = (2,3,4,1,5,0) my_str = 'cdbae' # 返回的都是list print(type(sorted(my_list))) # <class 'list'> print(sorted(my_list)) # [0, 1, 2, 3, 4, 5] 升序 print(sorted(my_list,reverse=True)) # [5, 4, 3, 2, 1, 0] 降序 print(type(sorted(my_tuple))) # <class 'list'> print(sorted(my_tuple)) # [0, 1, 2, 3, 4, 5] print(sorted(my_tuple,reverse=True)) # [5, 4, 3, 2, 1, 0] 降序 print(type(sorted(my_str))) # <class 'list'> print(sorted(my_str)) # ['a', 'b', 'c', 'd', 'e'] print(sorted(my_str,reverse=True)) # ['e', 'd', 'c', 'b', 'a'] # 接收一个key函数来实现自定义的排序 key_lilst = [22,-4,65,-34,43] new_key_list = sorted(key_lilst,key=abs) # 按照绝对值排序 print(new_key_list) # [-4, 22, -34, 43, 65] key_lilst_2 = [['b',1],['a',4],['d',3],['c',2]] new_key_list_2 = sorted(key_lilst_2,key=lambda x:x[0]) # x[0]表示元组里的第一个元素 print(new_key_list_2) # [['a', 4], ['b', 1], ['c', 2], ['d', 3]] print(sorted(key_lilst_2,key=lambda x:x[1])) # [['b', 1], ['c', 2], ['d', 3], ['a', 4]]
sort 与 sorted 区别:
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。 详细你可以参考列表的list.sort()方法
对于list,你可以使用 sorted 的 list.sort() 方法。list.sort()会修改原始的 list(返回值为None),而sorted()则返回新的list,原始list不变。通常list.sort()不如sorted()方便-如果你不需要原始的 list,list.sort()方法效率会稍微高一些。
代码实例:
my_list = [4,2,1,3] a1 = my_list.sort() print(a1) #None print(my_list) #[1, 2, 3, 4] my_list_2 = [4,2,1,3] a2 = sorted(my_list_2) print(a2) # [1, 2, 3, 4] print(my_list_2) # [4, 2, 1, 3] 原始的list并没有改变
最后修改:2020年3月24日 06:54