菜鸟教程《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
相关推荐
kobe_OKOK_5 小时前
DRF接口幂等操作
python·django
xian_wwq5 小时前
【学习笔记】-深度认知系列-第2讲-大模型到底是什么?——拆解“参数、训练、推理”
笔记·学习·深度认知
廿士5 小时前
python脚本使用相关
python
青 春 记 忆6 小时前
零基础入门python19:Flask账本第一步——应用工厂、蓝图和健康检查
python·flask·后端开发
朦胧之6 小时前
Python 后端核心知识
python
denggun123456 小时前
yield
前端·数据库·python
zx_741484817 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
Oll Correct8 小时前
Adobe illustrator 案例九:百宝箱图标绘制
笔记·ui·adobe·illustrator
招财小梗8 小时前
沈阳商贸公司AI企业服务优势几何?
大数据·人工智能·python
M78佐菲9 小时前
Linux学习笔记:进程
linux·笔记·学习·算法