前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python中的数据结构

python中的数据结构

作者头像
AngelNH
发布2020-04-15 09:37:52
6660
发布2020-04-15 09:37:52
举报
文章被收录于专栏:AngelNIAngelNI

对于习惯使用于C++的大佬来说, 容器的使用极大的方便了编程的需要,尤其对于参加算法竞赛的同学们,不必再自己去写类函数(当然了,类函数已经明明白白的)。作为python的使用者,开发者也为大家提供了已经打包好的函数库,import 即可。

今天为大家介绍一些python中数据结构的使用。


1.import queue

1)Queue classification

Queue classification

Feature

queue.Queue

先进先出

queue.LifoQueue

后进先出

queue.PriorityQueue

优先队列

queue.deque

双端队列

2)How to use?

Function

Explanation

put

放数据,Queue.put( )默认有block=True和timeout两个参数。当block=True时,写入是阻塞式的,阻塞时间由timeout确定。当队列q被(其他线程)写满后,这段代码就会阻塞,直至其他线程取走数据。Queue.put()方法加上 block=False 的参数,即可解决这个隐蔽的问题。但要注意,非阻塞方式写队列,当队列满时会抛出 exception Queue.Full 的异常

get

取数据(默认阻塞),Queue.get([block[, timeout]])获取队列,timeout等待时间

empty

如果队列为空,返回True,反之False

qsize

显示队列中真实存在的元素长度

maxsize

最大支持的队列长度,使用时无括号

join

实际上意味着等到队列为空,再执行别的操作

take_done

在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号

full

如果队列满了,返回True,反之False

clear

清空队列

3)Queue

代码语言:javascript
复制
import queue
q = queue.Queue(5) #定义一个长度为10 的队列,默认无限长
print(q.maxsize)
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get())
 ---------------------------
10
0
1
2
3
4

4)LifoQueue

代码语言:javascript
复制
import queue
lq = queue.LifoQueue(5) #定义一个长度为10 的队列,默认无限长
for i in range(5):
    lq.put(i)
while not q.empty():
    print(lq.get())
 ---------------------------
4
3
2
1
0

5)PriorityQueue

代码语言:javascript
复制
import queue
q = queue.PriorityQueue()
q.put((3,'aaaaa'))
q.put((3,'bbbbb'))
q.put((1,'ccccc'))
q.put((3,'ddddd'))
while not q.empty():
	print(q.get())
--------------------------
(1, 'ccccc')
(3, 'aaaaa')
(3, 'bbbbb')
(3, 'ddddd')

6)deque

代码语言:javascript
复制
import queue
dq = queue.deque()
dq.append(1)
dq.append(2)
dq.leftappend(100)
print(q)
print(q.pop())
print(q.popleft())
---------------------------
deque([100, 1, 2])
2
100

2.import heapq

1)How to use?

Function

Explanation

heappush(heap, x)

将x压入堆中

heappop(heap)

从堆中弹出最小的元素

heapify(heap)

让列表具备堆特征

heapreplace(heap, x)

弹出最小的元素,并将x压入堆中

nlargest(n, iter)

返回iter中n个最大的元素

nsmallest(n, iter)

返回iter中n个最小的元素

2)Example

代码语言:javascript
复制
import heapq
from random import shuffle
x = list(range(5))
shuffle(x)
heap = []
for i in x:
    heappush(heap,i)
print(heap)
heappush(heap,-10)
print(heap)
print(heappop(heap))
print(heapreplace(heap, 9))
print(heap)
-------------------
[0, 2, 1, 3, 4]
[-10, 2, 0, 3, 4, 1]
-10
0
[1, 2, 3, 4, 9]

3)Other

collections模块

collections模块实现一些特定的数据类型,可以替代Python中常用的内置数据类型如dict, list, set, tuple,简单说就是对基本数据类型做了更上一层的处理。

代码语言:javascript
复制
import collections
print(dir(collections))
-------------------------------------
['AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'ByteString', 'Callable', 'ChainMap', 'Collection', 'Container', 'Coroutine', 'Counter', 'Generator', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'OrderedDict', 'Reversible', 'Sequence', 'Set', 'Sized', 'UserDict', 'UserList', 'UserString', 'ValuesView', '_Link', '_OrderedDictItemsView', '_OrderedDictKeysView', '_OrderedDictValuesView', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_chain', '_class_template', '_collections_abc', '_count_elements', '_eq', '_field_template', '_heapq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr', '_repeat', '_repr_template', '_starmap', '_sys', 'abc', 'defaultdict', 'deque', 'namedtuple']
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-14|,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.import queue
    • 1)Queue classification
      • 2)How to use?
        • 3)Queue
          • 4)LifoQueue
            • 5)PriorityQueue
              • 6)deque
              • 2.import heapq
                • 1)How to use?
                  • 2)Example
                  • 3)Other
                    • collections模块
                    相关产品与服务
                    容器服务
                    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档