Python序列——列表
2016-07-16 22:45
363 查看
列表是什么
1 创建列表
2 访问列表和更新列表
列表相关操作
内建函数对列表的支持
1 cmp
2 序列类型函数
列表内建函数
列表应用
1 堆栈
2 队列
如果可以,尽量使用list.extend()方式代替连接操作符。
列表还支持非常重要的列表解析操作。
对两个列表的元素进行比较。
如果比较的元素是同类型的,则比较其值,返回结果。
如果两个元素不是同一类型的,则检查它们是否是数字。
3.1 如果是数字,执行必要的数字强制类型转换,然后比较。
3.2 如果有一方的元素是数字,则另一方的元素大。
3.3 否则,通过类型名字的字母顺序进行比较。
如果有一个列表首先到达末尾,则另一个长一点的列表大。
如果两个列表都到达结尾,且所有元素都相等,则返回0。
max()
min()
sorted()
reversed()
enumerate()
zip()
sum()
list()
tuple()
list.extend(x)
list.count(x)
list.index(x[, start[, end]])
list.insert(index, x)
list.pop([index])
list.remove(x)
list.remove()
list.sort([cmp[, key[, reverse]]])
运行示例如下:
运行示例如下:
1 创建列表
2 访问列表和更新列表
列表相关操作
内建函数对列表的支持
1 cmp
2 序列类型函数
列表内建函数
列表应用
1 堆栈
2 队列
1. 列表是什么
列表也是序列的一种。列表能保存任意数目的Python对象,列表是可变类型。1.1 创建列表
列表可以使用[]来创建,或者使用工厂方法list()来创建。>>> t = list() >>> type(t) <type 'list'> >>> l = [] >>> type(l) <type 'list'> >>> t == l True
1.2 访问列表和更新列表
>>> t = list('furzoom') >>> t ['f', 'u', 'r', 'z', 'o', 'o', 'm'] >>> t[1] 'u' >>> t[2] = 'n' >>> t ['f', 'u', 'n', 'z', 'o', 'o', 'm'] >>> t.append('.') >>> t ['f', 'u', 'n', 'z', 'o', 'o', 'm', '.'] >>> del t[3] >>> t ['f', 'u', 'n', 'o', 'o', 'm', '.'] >>> del t >>> t Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 't' is not defined
2. 列表相关操作
支持比较运算、切片[]或者[:]、in, not in、连接操作符+、重复操作。如果可以,尽量使用list.extend()方式代替连接操作符。
列表还支持非常重要的列表解析操作。
>>> [i for i in xrange(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3. 内建函数对列表的支持
3.1 cmp()
比较原则:对两个列表的元素进行比较。
如果比较的元素是同类型的,则比较其值,返回结果。
如果两个元素不是同一类型的,则检查它们是否是数字。
3.1 如果是数字,执行必要的数字强制类型转换,然后比较。
3.2 如果有一方的元素是数字,则另一方的元素大。
3.3 否则,通过类型名字的字母顺序进行比较。
如果有一个列表首先到达末尾,则另一个长一点的列表大。
如果两个列表都到达结尾,且所有元素都相等,则返回0。
3.2 序列类型函数
len()max()
min()
sorted()
reversed()
enumerate()
zip()
sum()
list()
tuple()
4. 列表内建函数
list.append(x)list.extend(x)
list.count(x)
list.index(x[, start[, end]])
list.insert(index, x)
list.pop([index])
list.remove(x)
list.remove()
list.sort([cmp[, key[, reverse]]])
5. 列表应用
5.1 堆栈
#!/usr/bin/env python # -*- coding: utf-8 -*- stack = [] def pushit(): stack.append(raw_input('Enter New string: ').strip()) def popit(): if len(stack) == 0: print 'Cannot pop from an empty stack!' else: print 'Removed [', `stack.pop()`, ']' def viewstack(): print stack CMDs = {'u': pushit, 'o': popit, 'v': viewstack} def showmenu(): pr = """ p(U)sh p(O)p (V)iew (Q)uit Enter choice: """ while True: while True: try: choice = raw_input(pr).strip()[0].lower() except (EOFError, KeyboardInterrupt, IndexError): choice = 'q' print '\nYou picked: [%s]' % choice if choice not in 'uovq': print 'Invalid option, try again' else: break if choice == 'q': break CMDs[choice]() if __name__ == '__main__': showmenu()
运行示例如下:
p(U)sh p(O)p (V)iew (Q)uit Enter choice: u You picked: [u] Enter New string: Python p(U)sh p(O)p (V)iew (Q)uit Enter choice: u You picked: [u] Enter New string: is p(U)sh p(O)p (V)iew (Q)uit Enter choice: u You picked: [u] Enter New string: cool! p(U)sh p(O)p (V)iew (Q)uit Enter choice: v You picked: [v] ['Python', 'is', 'cool!'] p(U)sh p(O)p (V)iew (Q)uit Enter choice: o You picked: [o] Removed [ 'cool!' ] p(U)sh p(O)p (V)iew (Q)uit Enter choice: o You picked: [o] Removed [ 'is' ] p(U)sh p(O)p (V)iew (Q)uit Enter choice: o You picked: [o] Removed [ 'Python' ] p(U)sh p(O)p (V)iew (Q)uit Enter choice: o You picked: [o] Cannot pop from an empty stack! p(U)sh p(O)p (V)iew (Q)uit Enter choice: ^D You picked: [q]
5.2 队列
#!/usr/bin/env python # -*- coding: utf-8 -*- queue = [] def enQ(): queue.append(raw_input('Enter New string: ').strip()) def deQ(): if len(queue) == 0: print 'Cannot pop from an empty queue!' else: print 'Removed [', `queue.pop(0)`, ']' def viewQ(): print queue CMDs = {'e': enQ, 'd': deQ, 'v': viewQ} def showmenu(): pr = """ (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: """ while True: while True: try: choice = raw_input(pr).strip()[0].lower() except (EOFError, KeyboardInterrupt, IndexError): choice = 'q' print '\nYou picked: [%s]' % choice if choice not in 'edvq': print 'Invalid option, try again' else: break if choice == 'q': break CMDs[choice]() if __name__ == '__main__': showmenu()
运行示例如下:
(E)nqueue (D)equeue (V)iew (Q)uit Enter choice: e You picked: [e] Enter New string: Bring out (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: e You picked: [e] Enter New string: your dead! (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: v You picked: [v] ['Bring out', 'your dead!'] (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: d You picked: [d] Removed [ 'Bring out' ] (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: d You picked: [d] Removed [ 'your dead!' ] (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: d You picked: [d] Cannot pop from an empty queue! (E)nqueue (D)equeue (V)iew (Q)uit Enter choice: ^D You picked: [q]
相关文章推荐
- python pip 报错KeyError: u'\u98de'处理
- Python 1
- Python错误和异常 学习笔记
- Python正则表达式 学习笔记
- django初体验 学习笔记
- Python进阶 学习笔记(三)
- Python进阶 学习笔记(二)
- Python进阶 学习笔记(一)
- Python入门 学习笔记
- 机器学习基础 维基翻译 保序回归 随机森林 Pipeline处理 及简单的sklearn例子
- 机器学习基础 维基翻译 超参数选择 K近邻法 及简单的sklearn例子
- Python笔记(4)类__属性与描述符
- scikit-learn svm初探
- scikit-learn 交叉验证绘图及原理实践
- 近似因子模型
- scikit-learn 回归基础
- python 奇异值分解小程序
- Python ~~~ 面向对象的利器
- python_数据库简单操作
- Learning Python 008 正则表达式-001