当前位置: 首页 > article >正文

【python】字典和数组的数组

一、字典是由键值对(key-value)组成的

因为 results[num] = {...} 这种写法是通过键(这里是 num)为 results 赋值,results 就是一个字典(dict)。
在 Python 里,字典是由键值对(key-value)组成的,像这样:

results = {}
results[1] = {"action_name": "xxx"}

这样 results 就是 {1: {"action_name": "xxx"}},每个 num 都对应一个字典。
而不是像列表那样用下标存放元素。

排序

       # Sort by the extracted numberresults.sort()

二、数组的数组

    ## 容易变为数组的数组#results = []##这是“数组的数组”(更准确地说,是 Python 的列表的列表),不是字典。
#每个内部列表包含多个元素(数字、字符串等),而不是键值对结构。results.append((num, action_name, duration, resource_id,action_vector_first))

排序

        #results = dict(sorted(results.items()))

使用

You can use this list of lists (array of arrays) by accessing elements with indexes. For example, to get the action name of the first item:

# results is your list of lists
action_name = results[0][1]  # "_片段010"
duration = results[0][2]     # "1.93s"
resource_id = results[0][3]  # "5984-3ac3a7ba2ef3"
vector = results[0][4]       # "10111111"

To loop through all items and print each action name:

for item in results:print(item[1])  # Prints the action_name

To find an item by number:

num_to_find = 11
for item in results:if item[0] == num_to_find:print(item)  # Prints the whole sublist for num 11

This structure is suitable for ordered data and easy iteration.


三、 二者对比

You are comparing two data structures for storing action information: a dictionary (dict) and a list of lists/tuples (an “array of arrays” or “list of lists/tuples” in Python).

1. Dictionary (dict)

  • Usage:
    results = {}
    results[num] = {"action_name": action_name,"duration": duration,"resource_id": resource_id,"action_vector_first": action_vector_first
    }
    
  • Features:
    • Fast lookup by num (the key).
    • Each entry is a dictionary, so you can access fields by name (e.g., results[23]["action_name"]).
    • Good for when you need to access items by a unique identifier or want to avoid duplicates.

2. List of Tuples (Array of Arrays)

  • Usage:
    results = []
    results.append((num, action_name, duration, resource_id, action_vector_first))
    
  • Features:
    • Ordered collection; you can sort or iterate in order.
    • Each entry is a tuple/list, so you access fields by index (e.g., results[0][1] for action_name).
    • Good for when order matters or you want to allow duplicates.

How to Choose

  • Use a dict if you want to look up actions by num quickly, or if num is unique.
  • Use a list of tuples if you care about order, want to allow duplicate num, or need to sort by different fields.

Example: Accessing Data

  • Dict:
    # Get action_name for num=23
    action_name = results[23]["action_name"]
    
  • List of Tuples:
    # Get action_name for the first item
    action_name = results[0][1]
    

Summary:

  • Use a dict for fast, key-based access and unique keys.
  • Use a list of tuples for ordered, index-based access and possible duplicates.
http://www.lryc.cn/news/2379233.html

相关文章:

  • 软考IPSEC案例分析
  • C++(23):容器类<vector>
  • Hugo 安装保姆级教程(搭建个人blog)
  • tomcat查看状态页及调优信息
  • 从坏道扫描到错误修复:HD Tune实战指南
  • 将嵌入映射到 Elasticsearch 字段类型:semantic_text、dense_vector、sparse_vector
  • 【LeetCode 热题100】17:电话号码的字母组合(详细解析)(Go语言版)
  • 解决uni-app开发中的“TypeError: Cannot read property ‘0‘ of undefined“问题
  • 翻译:20250518
  • 西门子1200/1500博图(TIA Portal)寻址方式详解
  • 《Python星球日记》 第78天:CV 基础与图像处理
  • 踩坑:uiautomatorviewer.bat 打不开
  • Atcoder Beginner Contest 406
  • 记录一次win11本地部署deepseek的过程
  • 嵌入式STM32学习——外部中断EXTI与NVIC的基础练习⭐
  • 进程状态并详解S和D状态
  • 数据获取_Python
  • <前端小白> 前端网页知识点总结
  • 历史数据分析——宁波海运
  • 小结:jvm 类加载过程
  • OpenCv高阶(八)——摄像头调用、摄像头OCR
  • Java开发经验——阿里巴巴编码规范实践解析3
  • MySQL——6、内置函数
  • MySQL如何查看某个表所占空间大小?(表空间大小查看方法)
  • 软件架构之-论软件系统架构评估以及应用
  • 低延迟与高性能的技术优势解析:SmartPlayer VS VLC Media Player
  • pytorch小记(十九):深入理解 PyTorch 的 `torch.randint()` 与 `.long()` 转换
  • 深入解析Spring Boot与微服务架构:从入门到实践
  • 【交互 / 差分约束】
  • 宝塔面板部署前后端项目SpringBoot+Vue2