Python 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
    reverse flag can be set to request the result in descending order.
	可以提供自定义键函数来自定义排序顺序,并且reverse可以设置为True按降序排序
	
	

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]






推荐

博客导读目录




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值