Python sorted和sort区别
程序员文章站
2022-04-21 10:40:13
sorted和sort源码比较用法比较待更新源码比较sorted(iterable, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. 从包含新项的iterable列表中升序返回。 A custom key function can be supplied to customise the sort order, and the...
源码比较
sorted(iterable, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
从包含新项的iterable列表中升序返回。
A custom key function can be supplied to customise the sort order, and the
reverse flag can be set to request the result in descending order.
可以提供自定义键函数来自定义排序顺序,并且反向标志可以设置为按降序请求结果
List.sort(key=None, reverse=False)
method of builtins.list instance
列表内置方法
用法比较
# 列表对比
print(a)
[6, 4, 5, 3, 7, 8, 1, 9, 2, 10]
print(a.sort()) # 返回None(对原列表操作,不会生成新列表)
None
print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(a.sort(reverse=True)) # 降序
None
print(a)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print(b)
[16, 17, 20, 11, 13, 14, 19, 18, 15, 12]
print(sorted(b)) # 返回排序后的列表(返回新列表,原数据不变)
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(sorted(b,reverse=True)) # 降序
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
print(id(b)) # id地址:2063835622088
print(id(sorted(b))) # id地址:2063833626376
# 其它数据类型对比
"""字符串"""
print(a)
'alsouefgb'
print(a.sort())
"""因sort是列表内置方法,所以字符串使用会报错"""
AttributeError: 'str' object has no attribute 'sort'
print(sorted(a))
['a', 'b', 'e', 'f', 'g', 'l', 'o', 's', 'u']
"""元组"""
print(b)
(1, 4, 5, 9, 2, 7, 3, 6)
print(sorted(b)) # 只要是可迭代的数据 皆可排序(只要有__iter__方法就是可迭代数据)
[1, 2, 3, 4, 5, 6, 7, 9]
"""字典"""
print(d)
{'c': 3, 'a': 9, 'b': 6}
print(d.items())
dict_items([('c', 3), ('a', 9), ('b', 6)])
print(sorted(d.items(),key=lambda x:x[1])) # 对字典按value排序
[('c', 3), ('b', 6), ('a', 9)]
print(sorted(d.items(),key=lambda x:x[0])) # 对字典按key排序
[('a', 9), ('b', 6), ('c', 3)]
# 补个小知识
a = 'abc' # 可以通过dir函数,查看它是否有iter方法
b = a.__iter__() # __iter__方法会返回迭代器(iterator)自身
print(b.__next__()) # __next__ 返回容器的下一个元素
'a'
print(b.__next__())
'b'
b = '123'
b.__next__() # b是可迭代对象,但不是迭代器,所以没有next方法
AttributeError: 'str' object has no attribute '__next__'
c = b.__iter__() # 调用iter方法, 返回迭代器
print(c)
<str_iterator object at 0x00000246E10AD5F8>
c.__next__() # 调用next, 返回容器的下一元素
'1'
# 参数key 对比(一致)
print(c)
[(10, 11), (9, 12), (8, 13), (7, 14), (6, 15), (5, 16), (4, 17), (3, 18), (2, 19), (1, 20)]
"""以每个元素的下标0来排序"""
print(c.sort(key=lambda x:x[0]))
None
print(c)
[(1, 20), (2, 19), (3, 18), (4, 17), (5, 16), (6, 15), (7, 14), (8, 13), (9, 12), (10, 11)]
print(c)
[(1, 20), (2, 19), (3, 18), (4, 17), (5, 16), (6, 15), (7, 14), (8, 13), (9, 12), (10, 11)]
"""以每个元素的下标1来排序"""
print(sorted(c,key=lambda x:x[1]))
[(10, 11), (9, 12), (8, 13), (7, 14), (6, 15), (5, 16), (4, 17), (3, 18), (2, 19), (1, 20)]
"""----------------分割线----------------"""
print(a)
[1, 3, 1, 5, 4, 2, 1, 3]
# 以重复次数排序(注意,这里的count不能加(),加()变成调用,会报错),降序,从多到少
print(sorted(a,key=a.count,reverse=True))
[1, 1, 1, 3, 3, 5, 4, 2]
print(sorted(a,key=a.count(),reverse=True))
TypeError: count() takes exactly one argument (0 given)
print(a)
[1, 3, 1, 5, 4, 2, 1, 3]
print(a.sort(key=a.count,reverse=True)) # 没有效果,来日再研究
None
print(a)
[1, 3, 1, 5, 4, 2, 1, 3]
待更新
本文地址:https://blog.csdn.net/su_zhen_hua/article/details/109644638
上一篇: 打造完美网吧---网吧技术参考方案
下一篇: 荐 轻松构建Tomcat源码