Python专家编程系列: 8. 高级数据结构介绍

0. 标题

Python专家编程系列: 8. 高级数据结构介绍

id:4

复制代码
作者: quantgalaxy@outlook.com   
blog: https://blog.csdn.net/quant_galaxy  
欢迎交流

1. 介绍

Python中,除了大家常用的数据结构外,还有几个非常好用的数据结构,这里主要介绍下Heap(堆),Deque(双端队列),Array(数组)。

2. 高级数据结构介绍

2.1 Heap(堆)

堆是一种基于树的数据结构,用于实现称为优先队列的抽象数据类型。

二叉树通常用于实现堆,堆主要有两种类型:

  1. 最小堆
    根结点的键值是所有堆结点键值中最小者的堆。
  2. 最大堆
    根结点的键值是所有堆结点键值中最大者的堆。

python提供了heapq模块,用于使用堆结构。

2.1.1 创建一个堆

python 复制代码
import heapq

lst = [1,5,3,6,2]

heapq.heapify(lst)

print(lst)
#[1, 2, 3, 6, 5]

2.1.2 给最小堆中添加一个元素

python 复制代码
heapq.heappush(lst, 10)
heapq.heappush(lst, 20)

print(lst)
#[1, 2, 3, 6, 5, 10, 20]

2.1.3 从最小堆中移除最小元素

python 复制代码
heapq.heappop(lst)
#1

print(lst)
# [2, 5, 3, 6, 20, 10]

2.1.4 查询堆中的最小的n个元素

python 复制代码
heapq.nsmallest(3, lst)
# [2, 3, 5]

2.1.5 查询堆中的n个最大元素

python 复制代码
heapq.nlargest(1, lst)
# [20]
复制代码
作者: quantgalaxy@outlook.com   
blog: https://blog.csdn.net/quant_galaxy  
欢迎交流

2.2 Deque(双端队列)

双端队列是栈和队列的泛化,允许从两端进行追加和弹出操作。

它比列表更可取,因为它在任何方向上的追加和弹出操作的时间复杂度都大约为0(1)。

另一方面,对于pop(0)和insert(0, v)操作,列表的时间复杂度为O(n)。

2.2.1 创建一个双端队列

python 复制代码
from collections import deque

# Create a new deque
d = deque([1, 2, 3, 4])

2.2.2 添加一个元素

python 复制代码
# Add to the right end
d.append(5)  

# Add to the left end
d.appendleft(0)

2.2.3 移除一个元素

python 复制代码
# Remove from the right end
rightmost = d.pop()  

# Remove from the left end
leftmost = d.popleft()

2.2.4 翻转一个双端队列

python 复制代码
print(d)
# deque([1,2,3,4])

d.reverse()

print(d)
# deque([4,3,2,1])
复制代码
作者: quantgalaxy@outlook.com   
blog: https://blog.csdn.net/quant_galaxy  
欢迎交流

2.3 Array(数组)

数组是一种数据结构,它将元素存储在连续的内存位置中。

虽然列表可以存储异构数据(不同类型的数据),但数组存储同构项(在初始化时需要类型规范),这使得它们对于统一数据的大型数据集来说内存效率更高。

数组存储同构项(在初始化时需要类型规范)。一定要记住,这是和list最大的不同。

当我们创建一个数组的时候,需要指定数组元素的类型,不同类型有不同的空间占用:

  • b: signed char (1 byte)
  • B: unsigned char (1 byte)
  • i: signed int (2 bytes)
  • f: float (4 bytes)
  • d: double (8 bytes)

2.3.1 创建一个数组

python 复制代码
from array import array 

# Create an array of integers
arr_int = array('i', [1, 2, 3, 4, 5])

# Create an array of floats
arr_float = array('f', [1.0, 2.1, 3.2])

2.3.2 往数组中添加一个元素

python 复制代码
arr_int.append(6)

2.3.3 加入多个元素

python 复制代码
arr_int.extend([7, 8, 9])

2.3.4 删除元素

python 复制代码
last_element = arr_int.pop()  # Removes and returns the last element

element_at_index_2 = arr_int.pop(2)  # Removes and returns the element at index 2

2.3.5 索引和切片操作

python 复制代码
print(arr_int[0])  # First element
print(arr_int[-1])  # Last element
print(arr_int[2:5])  # Elements from index 2 (inclusive) to 5 (exclusive)

注意,如果希望使用多维数组并对数组进行高级数学或统计操作,请选择Numpy数组。

3. 作者信息

复制代码
作者: quantgalaxy@outlook.com   
blog: https://blog.csdn.net/quant_galaxy  
欢迎交流
相关推荐
枫景Maple3 小时前
C#字典Dictionary的内部实现原理
开发语言·c#
张子夜 iiii4 小时前
(0️⃣基础)程序控制语句(初学者)(第3天)
人工智能·python
hrrrrb5 小时前
【Spring Boot 快速入门】八、登录认证(一)基础登录与认证校验
spring boot·后端
上单带刀不带妹6 小时前
Node.js 中的 fs 模块详解:文件系统操作全掌握
开发语言·javascript·node.js·fs模块
牵牛老人6 小时前
Qt中的QWebSocket 和 QWebSocketServer详解:从协议说明到实际应用解析
开发语言·qt·网络协议
有个人神神叨叨6 小时前
Cursor CLI 来了,准备 Build anything
ai·ai编程
chenglin0166 小时前
制造业ERP系统架构设计方案(基于C#生态)
开发语言·系统架构·c#
王大锤·6 小时前
基于spring boot的个人博客系统
java·spring boot·后端
凌晨7点6 小时前
控制建模matlab练习13:线性状态反馈控制器-②系统的能控性
开发语言·matlab
武文斌776 小时前
数据结构:哈希表、排序和查找
数据结构·散列表