1 bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 2 print(bicycles) 3 print(sorted(bicycles)) #默认第二参数为reverse = False,临时升序排序,并不改变原列表 4 print(sorted(bicycles, reverse = True)) #第二参数为reverse = True,临时降序排序,并不改变原列表 5 print(bicycles) 6 bicycles.sort() #默认第一参数为reverse = False,永久性升序排序,并改变原列表 7 print(bicycles) 8 bicycles.sort(reverse = True) #第一参数为reverse = True,永久性降序排序,并改变原列表 9 print(bicycles)
1 运行结果应该是: 2 ['trek', 'cannondale', 'redline', 'specialized'] 3 ['cannondale', 'redline', 'specialized', 'trek'] 4 ['trek', 'specialized', 'redline', 'cannondale'] 5 ['trek', 'cannondale', 'redline', 'specialized'] 6 ['cannondale', 'redline', 'specialized', 'trek'] 7 ['trek', 'specialized', 'redline', 'cannondale']