菜鸟教程《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
相关推荐
清水白石0083 小时前
Python 纯函数编程:从理念到实战的完整指南
开发语言·python
twilight_4693 小时前
机器学习与模式识别——机器学习中的搜索算法
人工智能·python·机器学习
was1723 小时前
你的私有知识库:自托管 Markdown 笔记方案 NoteDiscovery
笔记·云原生·自部署
Jia ming4 小时前
《智能法官软件项目》—罪名初判模块
python·教学·案例·智能法官
Jia ming4 小时前
《智能法官软件项目》—法律文书生成模块
python·教学·案例·智能法官软件
曦月逸霜4 小时前
Python数据分析——个人笔记(持续更新中~)
python
海棠AI实验室4 小时前
第六章 从“能用”到“能交付”的关键一刀:偏好对齐(Preference Alignment)数据工程
python·私有模型训练
浅念-5 小时前
C++ string类
开发语言·c++·经验分享·笔记·学习
百锦再5 小时前
Java多线程编程全面解析:从原理到实战
java·开发语言·python·spring·kafka·tomcat·maven
Jia ming5 小时前
《智能法官软件项目》—法律计算器模块
python·教学·案例·智能法官