The use of sort() method, sorted() function and reverse() method

这篇博客详细介绍了Python中用于列表排序的sort()方法、sorted()函数以及反转列表的reverse()方法。通过示例展示了如何实现字母顺序排序和反向排序,并且说明了这些操作对原始列表的影响。
摘要由CSDN通过智能技术生成

The use of sort() method, sorted() function and reverse() method
example:
places = ['peking', 'lijiang', 'xian', 'kunming', 'guilin']
print('Here is the original order:')
print(places)

print('\nHere is the alphabetical order:')
print(sorted(places))

print('\nHere is the original order again:')
print(places)

print('\nHere is the reverse alphabetical order:')
print(sorted(places, reverse=True))

print('\nThe list is still in its original order:')
print(places)

places.reverse()
print('\nHere is the reverse order:')
print(places)

places.reverse()
print('\nThe list is back to its original order:')
print(places)

places.sort()
print("\nUse sort() to change the list so it's stored in alphabetical order:")
print(places)

places.reverse()
print("\nUse reverse() to change the list so it's stored in reverse alphabetical order:")
print(places)

The result:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值