这一节将对你所学到的东西进行更深入的讲解。
5.1 关于List的更多介绍
list数据结构有很多的方法,以下是对这些方法的一些描述:
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
Remove the first item from the list whose value is x. It is an error if there is no such item.
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Return the number of times x appears in the list.
Sort the items of the list in place.
Reverse the elements of the list in place.
以下例子使用了list中的大多数方法:
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
你或许已经发现了诸如insert, remove或者sort这类方法修改了list中的数据,而且它们不会返回任何可打印的值(意味着这些方法返回的是None)。注意,这个是在Python中针对可变数据结构的一种设计原则。
5.1.1 将List作为堆栈来使用
list中的方法使得其作为堆栈来使用是非常容易的,堆栈意味着最后一个添加的数据是第一个被取到的(last in, and first out).为了添加一个数据到堆顶,可以使用append()方法。为了从堆顶取到一个数据,可以使用pop()方法(不需要指明所要取数据的Index)。比如:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
5.1.2 将List作为队列来使用
将list作为队列来使用也是可以的,这意味着第一个添加的数据将会第一个被取到(fist in, and first out)。尽管如此,list作为队列来使用并不是很有效的。对于list来说,在结尾处添加和获取数据是非常快速的,而在list起始处进行数据获取或插入是非常耗费时间的(因为所有后面的数据需要进行移位)。-NF:可见list在Python中的实现应该使用的是数组这种数据结构。
为了更好的实现一个队列,可以使用collections.deque,这个结构在两端进行插入或者删除操作都会有很好的效率。比如:
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
5.1.3 List解析
5.1.4 嵌套的List解析
5.2 del语句
5.3 元组和序列
5.4 集合
5.5 字典
5.6 循环技术
5.7 关于条件的更多介绍
5.8 序列和其它类型的比较
相关推荐
1、Python环境搭建 ...5、Python中的分支结构 5.1 单向分支-if语句 5.2 双向分支-if 5.3 多分支-if 5.4 分支结构的嵌套 5.5 条件表达式 6、Python中的循环结构 6.1 while循环 6.2 for循环 6.3 循环结构嵌套
1、Python环境搭建 ...5、Python中的分支结构 5.1 单向分支-if语句 5.2 双向分支-if 5.3 多分支-if 5.4 分支结构的嵌套 5.5 条件表达式 6、Python中的循环结构 6.1 while循环 6.2 for循环 6.3 循环结构嵌套
数据结构与算法(Python) 1. 引入概念 1.1. 第一次尝试 1.2. 算法的提出 1.3. 第二次尝试 1.4. 算法效率衡量 1.5. 算法分析 1.6. 常见时间复杂度 1.7. Python内置类型性能分析 1.8. 数据结构 2. 顺序表 2.1. 顺序表...
1.4Python 中复杂数据类型 第2章Python 基本语法 2.1数据运算 2.2选择结构 2.3循环结构 2.4for 循环 第3章函数 3.1自定义函数 3.2字符串函数 3.3集合类操作函数 3.4常用函数 第4章面向对象 4.1类和对象 4.2类的方法 ...
⼆、Python基本图形绘制 2.1深⼊理解python语⾔ 2.2实例2:python蟒蛇绘制 2.3 turtle库的使⽤ 2.4 turtle程序语法元素分析 三、基本数据类型 3.1 数据类型及操作 3.2 实例3:天天向上的⼒量 3.3 字符串类型及操作 ...
5 物理结构设计 17 5.1 物理结构 17 5.2 关系模式的存取方法 17 5.3 实现 17 5.4 表定义 17 6 数据库实施 22 6.1 数据输入 22 6.2 编码过程 23 7 系统调试、测试 26 7.1 系统功能测试 26 8 总结与心得 31 参考文献: ...
Python教程 1 关于作者 2 Python简介 2 1 安装Python 5 1.1 Python解释器 8 2 第一个Python程序 9 2.1 使用文本编辑器 10 2.2 输入和输出 14 3 Python基础 17 3.1 数据类型和变量 18 3.2 字符串和编码 25 3.3 使用...
2.1 B/S结构 5 2.2MySQL介绍 5 2.3MySQL环境配置 6 2.4Python语言简介 6 2.5Django框架 6 第三章 系统分析与设计 8 3.1系统说明 8 3.2系统可行性分析 8 3.2.1 技术可行性 8 3.2.2 经济可行性 8 3.2.3 操作可行性 9 ...
3.3 系统功能分析和描述 5 3.4系统UML用例分析 6 3.4.1管理员用例 6 3.4.2用户用例 6 3.5系统流程分析 6 3.5.1添加信息流程 6 3.5.2操作流程 7 3.5.3删除信息流程 8 第四章 系统设计 9 4.1 系统体系结构 9 4.2 ...
python数据分析/(印尼)伊德里斯(Idris.I.)著,韩波译。 资源包括所有章节的示例代码。需要用到python2和pip。 编辑推荐 实用的Python开源模块的大集合; 简单易懂、示例丰富的数据分析教程; 掌握数据可视化...
毕业设计:python django中学地理—江河湖泊学习(源码 + 数据库 + 说明文档) 2 相关技术介绍 3 2.1 Python语言 3 2.2 Django框架技术 3 2.3 B/S架构 3 3 需求分析与功能设计 4 3.1系统功能性需求分析 4 3.2系统的...
5 物理结构设计 17 5.1 物理结构 17 5.2 关系模式的存取方法 17 5.3 实现 17 5.4 表定义 17 6 数据库实施 22 6.1 数据输入 22 6.2 编码过程 23 7 系统调试、测试 26 7.1 系统功能测试 26 8 总结与心得 31 参考文献: ...
毕业设计:Python房屋信息可视化及价格预测系统(源码 + 数据库 + 说明文档) 2 开发工具及技术 2 2.1 B/S结构的介绍 2 2.2 Python技术的介绍 2 2.3 HTML技术的介绍 2 2.4 MYSQL数据库的介绍 3 2.5 开发环境的介绍 3...
毕业设计:python基于深度学习的交通标志识别系统(源码 + 数据库 + 说明文档) 2 开发工具及技术 2 2.1 B/S结构的介绍 2 2.2 PYTHON技术的介绍 2 2.3 HTML技术的介绍 2 2.4 MYSQL数据库的介绍 3 2.5 深度算法的介绍...
毕业设计:Python Django+Vue网上商城的设计与实现(源码 + 数据库 + 说明文档) 2 系统开发工具介绍 3 2.1 Python编程语言 3 2.2 Django开发工具 3 2.3 Vue框架 3 2.4 Mysql 数据库 3 3 可行性分析 4 3.1 技术的...