python中的队列取了还在吗_Python3优先级队列取出再放入之后内部顺便被打乱

你这个例子只是碰巧了。

优先队列是基于heapq(最小堆)的。了解一下最小堆的实现就明白了。

python中文文档里的8.5.3原理讲解http://python.usyiyi.cn/trans...

下面反例:

In [1]: from queue import PriorityQueue

In [2]: number = PriorityQueue(10)

In [3]: for i in range(8):

...: number.put(i)

...:

# 堆中的数据是存放在list中的,所以可以看到number.queue类型是list。

In [4]: number.queue?

Type: list

String form: [0, 1, 2, 3, 4, 5, 6, 7]

Length: 8

Docstring:

list() -> new empty list

list(iterable) -> new list initialized from iterable's items

In [5]: number.queue

Out[5]: [0, 1, 2, 3, 4, 5, 6, 7]

In [6]: number.put(-3)

In [7]: number.queue

Out[7]: [-3, 0, 2, 1, 4, 5, 6, 7, 3]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值