import heapq lst = list("AbSZDYM6BTXHU") print(lst) # ['A', 'b', 'S', 'Z', 'D', 'Y', 'M', '6', 'B', 'T', 'X', 'H', 'U'] heapq.heapify(lst) print(lst) # ['6', 'A', 'H', 'B', 'D', 'S', 'M', 'Z', 'b', 'T', 'X', 'Y', 'U'] print("最大三个", heapq.nlargest(3, lst)) # 最大三个 ['b', 'Z', 'Y'] ASCCI编码: b:98 Z:90 print("最小三个", heapq.nsmallest(3, lst)) # 最小三个 ['6', 'A', 'B'] ASCCI编码: 6:54 A:65
堆方法:
heapq.heappush(heap, item)
heapq.heappop(heap)
heapq.heapify(lst) 堆化
heapq.replace(heap, item) # 最小值替换为item
heapq.nlargest(3, lst)) heapq.nsmallest(3, lst)) 混合对根据 ASCCI编码大小排序标签:heapq,小写字母,ASCCI,混合,item,lst,heap,print From: https://www.cnblogs.com/sangern/p/17502601.html