python 数据结构常用的模块

来源:互联网 发布:ubuntu 内存使用情况 编辑:程序博客网 时间:2024/06/06 10:01

collections

collections.deque() 生成一个队列

>>>from collections import deque>>>q = deque()>>>q.append(1)>>>q.append(2)>>>q.appendleft(4)>>>q.pop()2>>>q.popleft()4
collections.deque(maxlen) 生成一个固定大小的队列,当队列满的时候,会自动删除原来的元素


heapq

heapq.nlargest(size,data)   返回data中最大的size个数

heapq.nsmallest(size,data)   返回data中最小的size个数

>>>from heapq import nlargest, nsmallest>>>data =[34,56,23,67,-90,2,6]>>>nlargest(2,data)[67,56]>>>nsmallest(3,data)[-90,2,6]


0 0
原创粉丝点击