代码:
lista=[20,40,30,50,10] listb=["bb","ee","aa","dd","cc"] #lista.sort() listc=sorted(lista) listd=sorted(lista,reverse=True) liste=sorted(listb) listf=sorted(listb,reverse=True) print(f"lista is {lista},asc sorted lista is: {listc}") print(f"lista is {lista},desc sorted lista is: {listd}") print(f"listb is {listb},asc sorted listb is: {liste}") print(f"listb is {listb},desc sorted listb is: {listf}")
效果:
lista is [20, 40, 30, 50, 10],asc sorted lista is: [10, 20, 30, 40, 50] lista is [20, 40, 30, 50, 10],desc sorted lista is: [50, 40, 30, 20, 10] listb is ['bb', 'ee', 'aa', 'dd', 'cc'],asc sorted listb is: ['aa', 'bb', 'cc', 'dd', 'ee'] listb is ['bb', 'ee', 'aa', 'dd', 'cc'],desc sorted listb is: ['ee', 'dd', 'cc', 'bb', 'aa']
总结:
- sort()默认是asc排序,desc需要加上reverse=True属性;
- 复制粘贴的时候注意修改里面对应的内容。