python中 .sort()、 sorted()和 .reverse() 、reversed()的区别
作者:互联网
原文链接:https://blog.csdn.net/larykaiy/article/details/82888383
现在举例说明:
a = [1,4,5,3,2,4,5,6,7,8]
b = a.sort()
print(a)
print(b)
#a.sort()改变了原有序列表a
- 1
- 2
- 3
- 4
- 5
[1, 2, 3, 4, 4, 5, 5, 6, 7, 8]
None
c = [1,4,5,3,2,4,5,6,7,8]
d = sorted(c)
print(c)
print(d)
#sorted(c)没改变原有序列表c,返回一个列表
- 1
- 2
- 3
- 4
- 5
[1, 4, 5, 3, 2, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 4, 5, 5, 6, 7, 8]
e = [1,4,5,3,2,4,5,6,7,8]
f = e.reverse()
print(e)
print(f)
#e.reverse()改变了原有序列表e
- 1
- 2
- 3
- 4
- 5
[8, 7, 6, 5, 4, 2, 3, 5, 4, 1]
None
g = [1,4,5,3,2,4,5,6,7,8]
h = reversed(g)
print(g)
print(h)
#reversed(g)没改变了原有序列表g,返回一个迭代器
- 1
- 2
- 3
- 4
- 5
[1, 4, 5, 3, 2, 4, 5, 6, 7, 8]
标签:sort,reverse,python,reversed,print,sorted,12345,序列表 来源: https://blog.csdn.net/fafawf/article/details/101156271