Sort
Method One
1. Sorting by numbers (from small to large, changing itself)
scores = [23,43,54,67,87,100] scores.sort() print(scores)
2. Sorting strings (alphabetically)
**方法一 .sort()** name = ["shi","sheng","ru","yi","huang","meng"] name.sort() print(name) **方法二 sorted()函数(返回一个新的结果,列表的本身不会变化)** a = [34,1,43,99,78] a = ["shi","sheng","ru","yi","huang","meng"] print(sorted(a)) print(a)
3. Sorting strings (sorted by end letter)
name = ["shi","sheng","ru","yi","huang","meng"] name.sort(key=lambda a:a[-1]) print(name)
Inverted list
**方法一** scores = [23,43,54,3,87,100] scores.sort(reverse=True) #(先从大到小排序再倒置) print(scores) **方法二** scores = [23,43,54,3,87,100] scores.reverse() #(直接倒置) print(scores)
Comments:
1. Sort the list. Sort ()
2. If it is a letter, sort it by the first letter
3. If you want to sort by the last letter, list. Sort (Key=lambda a:a[-1])
4. List of inverted sequences. Sort (reverse=true)
5. Inverted sequence list. Reverse ()
sorted() 函数 按照从小到大进行排序,会返回一个新的结果,本身不会发生变化
Sort columns (list)