菜鸟教程《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
相关推荐
qq_417695052 分钟前
构建一个桌面版的天气预报应用
jvm·数据库·python
cm6543203 分钟前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
小陈的进阶之路6 分钟前
Selenium 常用操作 API
python·自动化·pytest
存储服务专家StorageExpert7 分钟前
NetApp NVME SSD 盘的学习笔记
运维·服务器·笔记·学习·存储维护·emc存储·netapp
2301_816651229 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
无聊大侠hello world19 分钟前
黑马大模型 RAG 与 Agent 实战学习笔记
笔记·学习
2501_9181269120 分钟前
学习所有python写服务器的语句
服务器·人工智能·python·学习·个人开发
承渊政道21 分钟前
【优选算法】(实战感悟二分查找算法的思想原理)
c++·笔记·学习·算法·leetcode·visual studio code
m0_5698814726 分钟前
使用Python自动收发邮件
jvm·数据库·python
极光代码工作室28 分钟前
基于机器学习的房价预测系统设计与实现
人工智能·python·深度学习·机器学习