菜鸟教程《Python 3 教程》笔记(15):数据结构

菜鸟教程《Python 3 教程》笔记(15)

  • [15 数据结构](#15 数据结构)
    • [15.1 将列表当作队列使用](#15.1 将列表当作队列使用)
    • [15.2 遍历技巧](#15.2 遍历技巧)

笔记带有个人侧重点,不追求面面俱到。

15 数据结构

出处: 菜鸟教程 - Python3 数据结构

15.1 将列表当作队列使用

在列表的最后添加或者弹出元素速度快,然而在列表里插入或者从头部弹出速度却不快(因为所有其他的元素都得一个一个地移动)。

15.2 遍历技巧

遍历字典:

python 复制代码
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
...     print(k, v)
...
gallahad the pure
robin the brave

遍历列表:

python 复制代码
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...     print(i, v)
...
0 tic
1 tac
2 toe

遍历多个序列:

python 复制代码
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}?  It is {1}.'.format(q, a))
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

反向遍历:

python 复制代码
>>> for i in reversed(range(1, 10, 2)):
...     print(i)
...
9
7
5
3
1
相关推荐
不是万能的豆学长4 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
gCode Teacher 格码致知5 小时前
Pandas教学-18:number与numeric的区别
python·pandas
梦想不只是梦与想8 小时前
Python 中subprocess 模块:执行外部命令
python·subprocess·调用外部命令
懂点Tech8 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
ACP广源盛139246256738 小时前
Type-C 扩展坞方案选型笔记:DP1.4 转 HDMI2.0 桥接方案评估@ACP
笔记·硬件架构·hdmi·国产芯片·扩展坞
ss27311 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 22 PostgreSQL实例与集群
数据库·笔记·postgresql
ss27311 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 21 PostgreSQL进程与内存架构介绍
数据库·笔记·postgresql
爱上纯净的蓝天13 小时前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
cspttty13 小时前
2027风险管理岗准备路线:数学与应用数学如何补齐金融、SQL与Python
开发语言·python·金融
用户83562907805113 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python