- 列表简介:
- 列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等
- 列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作
- 可以通过list(seq)函数把一个序列类型转换成一个列表
- 列表对象支持的方法:
- 1.append(x),在列表尾部追加单个对象x,使用多个参数会引起异常
- 2.count(x),返回对象x在列表中出现的次数
- 3.extend(L),将列表L中的表项添加到列表中,返回None
- 4.Index(x),返回列表中匹配对象x的第一个列表项的索引,无匹配元素时产生异常
- 5.insert(i,x),在索引为i的元素前插入对象x,如list.insert(0,x)在第一项前插入对象,返回None
- 6.pop(x),删除列表中索引为x的表项,并返回该表项的值,若未指定索引,pop返回列表最后一项
- 7.remove(x),删除列表中匹配对象x的第一个元素,匹配元素时产生异常,返回None
- 8.reverse(),颠倒列表元素的顺序
- 9.sort(),对列表排序,返回none,bisect模块可用于排序列表项的添加和删除
- 几个实例:
- 1.获取有关 list 的帮助
01.>>> help(list) 02.Help on class list in module __builtin__: 03.class list(object) 04.| list() -> new list 05.| list(sequence) -> new list initialized from sequence's items 06.| 07.| Methods defined here: 08.| 09.| __add__(...) 10.| x.__add__(y) <==> x+y 11.| 12.| __contains__(...) 13.| x.__contains__(y) <==> y in x 14.| 15....
- 2.本例展示如何创建包含从0到9(包括0和9)的简单list,以及如何创建一个空列表和一个包含单个条目的列表
01.>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 02.>>> l 03.[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 04.>>> type(l) 05.<type 'list'> 06.>>> el = [] # Create an empty list 07.>>> len(el) 08.0 09.>>> sl = [1] # Create a single item list 10.>>> len(sl) 11.1
- 3.直接创建 list 对象,将序列直接传递给构造函数,还可以将拥有元组或字符串的变量传递给 list 构造函数
01.>>> l = list() 02.>>> type(l) 03.<type 'list'> 04.>>> len(l) 05.0 06.>>> l 07.[] 08.>>> l = list((0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) # Create a list from a tuple 09.>>> l 10.[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 11.>>> len(l) 12.10 13.>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Create a list from a list 14.>>> l 15.[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 16.>>> len(l) 17.10 18.>>> l = list(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 19.# Error: Must pass in a sequence 20.Traceback (most recent call last): 21. File "<stdin>", line 1, in ? 22.TypeError: list() takes at most 1 argument (10 given) 23.>>> l = list("0123456789") # Create a list from a string 24.>>> l 25.['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 26.>>> type(l) 27.<type 'list'> 28.>>> len(l) 29.10
- 4.从 list 访问条目
01.>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 02.>>> l[0] # Get the first item in the list 03.0 04.>>> type(l[0]) 05.<type 'int'> 06.>>> l[5] # Get the sixth item in the list 07.5 08.>>> l[1:5] # Get the second through fifth items 09.[1, 2, 3, 4] 10.>>> type(l[1:5]) 11.<type 'list'> 12.>>> l[0::2] # Get every second item 13.[0, 2, 4, 6, 8] 14.>>> l[0], l[1], l[2] 15.(0, 1, 2)
- 注:
- 切片是一个非常有用的概念,其一般形式为 l[start:end:step],其中 start 和 end 分别是开始和结束索引,step 是在切片时要跨过的条目数量
- 此外,还可以对结束索引使用负值,即从序列的结尾往回计数
- 另一个有用的功能是以一种很合适的方式处理错误(如超过序列的长度),如前一个例子所示,还可以选择忽略切片中使用的三个值中的一个或多个值
- 5.可变的序列,修改 list
- 本文的开头,提到过list 是一个可变的序列,这就意味着您不但可以方便地访问 list 中的条目,而且可以方便地修改它们,但这会引起一个并发症状:您只能修改序列中的条目,若要向序列中添加条目(而不仅仅是修改条目),可使用 append 方法
01.>>> l = [] 02.>>> l[0] = 0 # The list is empty 03.Traceback (most recent call last): 04. File "<stdin>", line 1, in ? 05.IndexError: list assignment index out of range 06.>>> l.append(0) 07.>>> l 08.[0] 09.>>> l[0] = 1 10.>>> l 11.[1]
- 注:
- 如本例所演示的,尝试修改不存在的 list条目会导致出现错误
- 本文的开头,提到过list 是一个可变的序列,这就意味着您不但可以方便地访问 list 中的条目,而且可以方便地修改它们,但这会引起一个并发症状:您只能修改序列中的条目,若要向序列中添加条目(而不仅仅是修改条目),可使用 append 方法
- 6.异构的可变序列
- list可以持有不同类型的数据(或不同类型的对象),即异构的可变序列
01.>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 02.>>> l 03.[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 04.>>> l[2] = 2 05.>>> type(l[2]) 06.<type 'int'> 07.>>> l[2] = "two" # Change the type of an element 08.>>> type(l[2]) 09.<type 'str'> 10.>>> l 11.[0, 1, 'two', 3, 4, 5, 6, 7, 8, 9] 12.>>> l[2] = l[2:5] * 2 13.>>> l 14.[0, 1, ['two', 3, 4, 'two', 3, 4], 3, 4, 5, 6, 7, 8, 9] 15.>>> del(l[2]) # Remove single element 16.>>> l 17.[0, 1, 3, 4, 5, 6, 7, 8, 9] 18.>>> l[1:3] = [] # Remove a slice 19.>>> l 20.[0, 4, 5, 6, 7, 8, 9]
- 注:
- 本例展示了如何向 list 中添加元素,以及如何修改 list 中的条目
- 还演示了如何从 list 中删除对象;删除条目的第一个方法是使用 del 方法,使用此方法可以删除一个条目或一个条目范围;还可以使用灵活而强大的切片方法从 list 中删除切片
- list可以持有不同类型的数据(或不同类型的对象),即异构的可变序列
- 1.获取有关 list 的帮助
Python的列表(list)介绍
最新推荐文章于 2024-03-04 16:44:02 发布