Python学习(五)——列表操作全透析
2016-04-21 13:56
661 查看
列表是以类的形式实现的。“创建”列表实际上是将一个类实例化。因此,列表有多种方法可以操作。Python列表操作的函数和方法
1、cmp(list1, list2):比较两个列表的元素
2、len(list):列表元素个数
3、max(list):返回列表元素最大值
4、min(list):返回列表元素最小值
5、list(seq):将元组转换为列表
1、list.append(obj):在列表末尾添加新的对象
2、list.count(obj):统计某个元素在列表中出现的次数
3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
5、list.insert(index, obj):将对象插入列表
6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7、list.remove(obj):移除列表中某个值的第一个匹配项
8、list.reverse():反向列表中元素
9、list.sort([func]):对原列表进行排序
列表操作包含以下函数: |
2、len(list):列表元素个数
3、max(list):返回列表元素最大值
4、min(list):返回列表元素最小值
5、list(seq):将元组转换为列表
列表操作包含以下方法: |
2、list.count(obj):统计某个元素在列表中出现的次数
3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
5、list.insert(index, obj):将对象插入列表
6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7、list.remove(obj):移除列表中某个值的第一个匹配项
8、list.reverse():反向列表中元素
9、list.sort([func]):对原列表进行排序
列表操作包含以下方法: |
简单的实例 |
>>> #list.append(n),追加元素,只接收一个参数 >>> a [0, 1, 2, 3, 4, 5, 6, 7] >>> a.append(6) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6]
>>> #list.count(n) , 计算n在list中出现的次数 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6] >>> a.count(6) 2
>>> #list.extend(list1) , 将list1追加到list的后面 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6] >>> b = ['a' , 'b' , 'c' , 'd'] >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6] >>> a.extend(b) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd'] >>> a.append(b) #可以注意append与extend的区别 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> #list.index(n) , 返回n在list中的位置,若无,则抛出异常 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6] >>> a.index('x') Traceback (most recent call last): File "<pyshell#67>", line 1, in <module> a.index('x') ValueError: 'x' is not in list >>> a.index(4) 4
>>> #list.insert(index,var) , 在index出插入var,其余元素向后推,如果index大于list的长度,就会在后面添加。如果index小于0,就要在最开始出添加 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']] >>> a.insert(0,1) >>> a [1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']] >>> a.insert(100,100) >>> a [1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd'], 100] >>>
>>> #list.pop() , 返回最后一个元素,并且删除最后一个元素。list.pop(index) , 返回index处的元素,并且删除该元素。 >>> a [1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd',100] >>> a.pop() 100 >>> a [1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd',] >>> a.pop(0) 1 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd']
>>> #list.remove(var) , 找到var并且删除它,若无,则抛出异常 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd'] >>> a.remove(9) Traceback (most recent call last): File "<pyshell#98>", line 1, in <module> a.remove(9) ValueError: list.remove(x): x not in list >>> a.remove(6) >>> a [0, 1, 2, 3, 4, 5, 7, 6, 'a', 'b', 'c', 'd']
>>> #list.reverse() , 将list倒序 >>> a [0, 1, 2, 3, 4, 5, 7, 6, 'a', 'b', 'c', 'd'] >>> a.reverse() >>> a ['d', 'c', 'b', 'a', 6, 7, 5, 4, 3, 2, 1, 0]
>>> #list.sort() , 将list进行排序,a中元素若类型不同,结果自己看看一下,但是一般不会这么做 >>> a ['d', 'c', 'b', 'a', 6, 7, 5, 4, 3, 2, 1, 0] >>> a.sort() Traceback (most recent call last): File "<pyshell#107>", line 1, in <module> a.sort() TypeError: unorderable types: int() < str() >>> a = [1,3,2,4,5,6,3,2,1] >>> a [1, 3, 2, 4, 5, 6, 3, 2, 1] >>> a.sort() >>> a [1, 1, 2, 2, 3, 3, 4, 5, 6]
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法