python3排序
1 # 冒泡排序 2 array = [1, 6, 7, 2, 9, 4] 3 for i in range(len(array) - 1, 1, -1): 4 for j in range(0, i): 5 if array[j] > array[j + 1]: 6 array[j], array[j + 1] = array[j + 1], array[j] 7 print(array) 8 9 array.sort() 10 print(array)
1 # 字典排序 2 dict1 = {'a': 2, 'e': 3, 'f': 8, 'd': 4} 3 list1 = sorted(dict1.items(), key=lambda x: x[1]) 4 print(list1)
1 import operator 2 # 列表中的字典排序 3 alist = [{"name": "a", "age": 20}, {"name": "b", "age": 30}, {"name": "c", "age": 25}] 4 sorted_x = sorted(alist, key=operator.itemgetter('age')) 5 print(sorted_x)
1 # 统计出现次数 2 a = ['apple', 'banana', 'apple', 'tomato', 'orange', 'apple', 'banana', 'watermeton'] 3 print(a.count('apple'))
1 # 合并为字典 2 a = ['a', 'b', 'c', 'd', 'e'] 3 b = [1, 2, 3, 4, 5] 4 c = dict(zip(a, b)) 5 print(c)