第五章 数据结构4

第五章 数据结构4

相关代码如下

from array import array    # 用于数组内容
from collections import deque   # 用于队列内容


# 堆
# 所谓堆,是指最后一个进入,第一个离开
browsing_session = []
browsing_session.append(1)
browsing_session.append(2)
browsing_session.append(3)
print(browsing_session)
last = browsing_session.pop()
print(last)
print(browsing_session)
print("redirect", browsing_session)
if not browsing_session:
    print(browsing_session[-1])
    print("disable")

# 队列
# 队列是指第一个进第一个出
queue = deque([])   # 注意deque([]),deque是一种类似列表的函数
queue.append(1)
queue.append(2)
queue.append(3)
queue.popleft    # pop()函数可更改默认删除方向
print(queue)
if not queue:
    print("empty")

# 元组
point = (1, 2) + (3, 4)*2
point = tuple([1, 2])   # tuple将列表改为元组
point = tuple("Hello world")
point = (1, 2, 3)
# 若删除括号,python默认为元组,并且末尾为逗号,则python默认为元组
print(point[0:2])
x, y, z = point
# 元组不可更改

# 变换变量值
x = 10
y = 11

x, y = y, x    # 等号后面是一个元组
print(x, y)


# 数组
# 除非是处理大量数据导致优化出现问题,否则不要轻易使用数组

numbers = array("i", [1, 2, 3])
# “i”是数据类型,如这里面是整数型,就不可使用浮点数
# numbers.append(4)
# numbers.insert(4)    # 向指定位置加入内容
# numbers.pop(4)
# numbers.remove(4)

输出内容

[1, 2, 3]
3
[1, 2]
redirect [1, 2]
deque([1, 2, 3])
(1, 2)
11 10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值