字符串sorted,和sort区别

s=["a","s","b"]
s.sort()
print(s)
['a', 'b', 's']
s=["a","s","b"]
s.reverse()
print(s)
['b', 's', 'a']
s=["a","s","b"]
sorted(s)
Out[9]: ['a', 'b', 's']
print(s)
['a', 's', 'b']

  sorted排序后不改变原序列

.sort()和.reverse()函数都是永久性改变,直接改变原列表中的元素

reverse()函数将原列表中的元素倒置

sort(reverse=True)先对列表中的元素进行排序后再进行倒置

s.sort(reverse=True)
print(s)
['s', 'b', 'a']

  

猜你喜欢

转载自www.cnblogs.com/qiu-1010/p/10700390.html