一、列表概述
list是Python中最基本的数据结构,其是以方括号“[]”包围的数据集合,不同成员以“,”分隔的一组有序项目的集合 。列表内的成员可通过序号访问,列表中可以包含任何数据类型,也可包含另一个列表【可任意组合嵌套】,list列表是可变的数据类型【可进行增删改查】。列表的内部函数可通过dir(list)查看,具体每个函数的作用可能过help(list)进行查看。
二、创建list列表
可以通过方括号创建一个列表,也可以通过内建函数list 将一个字符串转化为列表 ,同样可以通过字符串分隔函数转化为列表。示例如下:
>>> list1 = [1,2,3,4] >>> print list1 [1, 2, 3, 4] >>> list2 = list(range(4)) >>> print list2 [0, 1, 2, 3] >>> list3 = '1,2,3,4,5'.split(",") >>> print list3 ['1', '2', '3', '4', '5'] >>> list4 = list('hello 361way') >>> print list4 ['h', 'e', 'l', 'l', 'o', ' ', '3', '6', '1', 'w', 'a', 'y']
三、list列表的索引与切片
同python下的字符串索引一样,list也的正向索引默认也是从0开始,反向索引从 -1开始。list同string一样,也支持切片,用法和string下的用法也相同,如下:
>>> list1[0] 1 >>> list1[2] 3 >>> list1[1:3] [2, 3] >>> list1[1:-1] [2, 3] >>> list1[1:-1:2] [2] >>> list1[0:-1:2] [1, 3] >>>
通过索引切片也可以很方便的复制一个list,如:l2 = l1[:] 。
列表元素的变更与删除:
>>> list = ['physics', 'chemistry', 1997, 2000]; >>> print list ['physics', 'chemistry', 1997, 2000] >>> list[1]='361way' >>> print list ['physics', '361way', 1997, 2000] >>> del list[2] >>> print list ['physics', '361way', 2000] >>> list[3]='com' Traceback (most recent call last): File "", line 1, in >>> del list[1:] >>> print list ['physics']IndexError: list assignment index out of range
可以通过list[索引号]=‘‘元素值’’ 的方式修改一个元素的结果,也可以通过del list[索引号]的方式删除一个或一组元素。
四、list列表操作符
list列表也支持len、max、min、+、* 操作,这与字符串相似。如下:
Python 表达式 | 结果 | 描述 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
min([1, 2, 3]) | 1 | 最小元素值 |
max([1, 2, 3]) | 3 | 最大元素值 |
[1, 2, 3 ,4] + [4, 5, 6 ,3] | [1, 2, 3, 4, 4, 5, 6 ,3] | 组合 |
[‘Hi!’] * 4 | [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] | 重复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print x, | 1 2 3 | 迭代 |
五、list的方法函数
list 列表包含如下方法:
list.insert(index,var) list.pop(var) #返回最后一个元素,并从list中删除之 list.remove(var) #删除第一次出现的该元素 list.count(var) #该元素在列表中出现的个数 list.index(var) #该元素的位置,无则抛异常 list.append(var) #追加元素 list.extend(list) #追加list,即合并list到L上 list.sort() #排序 list.reverse() #倒序
示例如下:
>>> l1 = [5,3,2,1,4,6] >>> l1.sort() >>> print l1 [1, 2, 3, 4, 5, 6] >>> list = ['physics', '361way', 1997, 2000] >>> list.sort() >>> print list [1997, 2000, '361way', 'physics'] >>> list.index(1997) >>> list.index(2000) 1 >>> list.index(a) //查找字符串时,需要用引号引起来 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> list.index("a") //index索引不到查询的值时抛出异常 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'a' is not in list >>> 1 in l1 //支持in 和 not in 判断操作 True >>> 1 not in l1 False
这里需要单独说明下list的extend(扩展)方法 与 append(追加)方法的区别,先看示例:
>>> li = ['a', 'b', 'c'] >>> li.extend(['d', 'e', 'f']) >>> li ['a', 'b', 'c', 'd', 'e', 'f'] >>> len(li) 6 >>> li[-1] 'f' >>> li = ['a', 'b', 'c'] >>> li.append(['d', 'e', 'f']) >>> li ['a', 'b', 'c', ['d', 'e', 'f']] >>> len(li) 4 >>> li[-1] ['d', 'e', 'f']
两者的区别:append可以接受任何类型的数据,并简单地将数据追加到list尾部;extend只能按受list参数,将把list内的每个元素加到原list 中。再来一个例子看下:
>>> print list [1997, 2000, '361way', 'physics'] >>> list.append('com') >>> print list [1997, 2000, '361way', 'physics', 'com'] >>> list.append('test','111') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: append() takes exactly one argument (2 given) >>> list.append(('test','111')) >>> print list [1997, 2000, '361way', 'physics', 'com', ('test', '111')]
六、list列表的遍历枚举
>>> print list [1997, 2000, '361way', 'physics', 'com', ('test', '111')] //直接遍历 >>> for i in list: ... print i ... 1997 2000 361way physics com ('test', '111') //索引枚举 >>> for index,value in enumerate(list): ... print index,value ... 0 1997 1 2000 2 361way 3 physics 4 com 5 ('test', '111')
注:上面使用到了 python内部函数enumerate ,该函数相当强大,可以枚举string、list、dict 、tuple 。