菜鸟教程《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
相关推荐
嗯嗯=20 小时前
python学习篇
开发语言·python·学习
WoY202020 小时前
opencv-python在ubuntu系统中缺少依赖
python·opencv·ubuntu
大游小游之老游1 天前
Python中如何实现一个程序运行时,调用另一文件中的函数
python
mantch1 天前
个人 LLM 接口服务项目:一个简洁的 AI 入口
人工智能·python·llm
weixin_445054721 天前
力扣热题51
c++·python·算法·leetcode
朱朱没烦恼yeye1 天前
java基础学习
java·python·学习
lkbhua莱克瓦241 天前
进阶-索引3-性能分析
开发语言·数据库·笔记·mysql·索引·性能分析
databook1 天前
数据可视化五大黄金原则:让你的图表“会说话”
python·数据分析·数据可视化
ai_top_trends1 天前
2026 年度工作计划 PPT 模板与 AI 生成方法详解
人工智能·python·powerpoint
智航GIS1 天前
9.4 Word 自动化
python·自动化·word