菜鸟教程《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
相关推荐
jiang_changsheng4 分钟前
RTX 2080 Ti魔改22GB显卡的最优解ComfyUI教程
python·comfyui
wdfk_prog31 分钟前
[Linux]学习笔记系列 -- [drivers][I2C]I2C
linux·笔记·学习
0思必得044 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
沈浩(种子思维作者)1 小时前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算
2301_790300961 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
m0_736919101 小时前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
getapi1 小时前
实时音视频传输与屏幕共享(投屏)
python
觉醒大王1 小时前
哪些文章会被我拒稿?
论文阅读·笔记·深度学习·考研·自然语言处理·html·学习方法
方安乐2 小时前
科普:股票 vs 债券的区别
笔记
java干货2 小时前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法