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 创建一个堆

import heapq

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

heapq.heapify(lst)

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

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

heapq.heappush(lst, 10)
heapq.heappush(lst, 20)

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

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

heapq.heappop(lst)
#1

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

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

heapq.nsmallest(3, lst)
# [2, 3, 5]

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

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 创建一个双端队列

from collections import deque

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

2.2.2 添加一个元素

# Add to the right end
d.append(5)  

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

2.2.3 移除一个元素

# Remove from the right end
rightmost = d.pop()  

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

2.2.4 翻转一个双端队列

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 创建一个数组

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 往数组中添加一个元素

arr_int.append(6)

2.3.3 加入多个元素

arr_int.extend([7, 8, 9])

2.3.4 删除元素

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 索引和切片操作

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  
欢迎交流

相关推荐

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

    2024-01-11 13:14:02       42 阅读
  2. [python高级编程]:01-数据结构

    2024-01-11 13:14:02       62 阅读
  3. Python高级数据结构——树(Tree)

    2024-01-11 13:14:02       62 阅读
  4. Python高级数据结构——字典树(Trie)

    2024-01-11 13:14:02       66 阅读
  5. Python高级数据结构——堆(Heap)

    2024-01-11 13:14:02       59 阅读
  6. Python高级编程Python中Excel表格处理数据

    2024-01-11 13:14:02       32 阅读
  7. Python高级编程数据分析与数据可视化

    2024-01-11 13:14:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 13:14:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 13:14:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 13:14:02       87 阅读
  4. Python语言-面向对象

    2024-01-11 13:14:02       96 阅读

热门阅读

  1. What does `rpm -e` do?

    2024-01-11 13:14:02       58 阅读
  2. C++ 异常处理

    2024-01-11 13:14:02       39 阅读
  3. 浅谈Vue2与Vue3的双向绑定原理的理解

    2024-01-11 13:14:02       45 阅读
  4. android:clickable=“false“无效,依然能被点击

    2024-01-11 13:14:02       43 阅读
  5. ROS2学习笔记二:开发准备

    2024-01-11 13:14:02       50 阅读
  6. 【算法】费解的开关(递推)

    2024-01-11 13:14:02       55 阅读
  7. 力扣_数组26—合并两个有序数组

    2024-01-11 13:14:02       52 阅读
  8. ubuntu22安装NetworkManager

    2024-01-11 13:14:02       51 阅读