【Python基础教程】第2章 列表和元组-2.3 列表

来源:互联网 发布:oracle11 抓取 sql 编辑:程序博客网 时间:2024/05/20 06:05

列表是可变的,意味着在列表创建之后可以改变列表的内容。

2.3.1 list函数

>>> #2.3.1 list函数>>> #根据字符串创建列表>>> listA = list('Hello')>>> listA['H', 'e', 'l', 'l', 'o']>>> #将字符组成的列表转换为字符串>>> strA = ''.join(listA)>>> strA'Hello'
2.3.2 基本的列表操作

>>> #2.3.2 基本的列表操作>>> #1.改变列表:元素赋值>>> x = [1, 1, 1]>>> x[1] = 2>>> x[1, 2, 1]>>> #2.删除元素>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']>>> del names[2]#使用del语句进行删除>>> names['Alice', 'Beth', 'Dee-Dee', 'Earl']>>> #3.分片赋值>>> #(1)简单多元素赋值>>> name = list('Perl')>>> name['P', 'e', 'r', 'l']>>> name[2:] = list('ar')>>> name['P', 'e', 'a', 'r']>>> #(2)分片替换:与原序列不等长也可以>>> name = list('Perl')>>> name[1:] = list('ython')>>> name['P', 'y', 't', 'h', 'o', 'n']>>> #(3)插入新的元素>>> numbers = [1, 5]>>> numbers[1:1] = [2, 3, 4]>>> numbers[1, 2, 3, 4, 5]>>> #(4)删除元素>>> numbers[1, 2, 3, 4, 5]>>> numbers[1:4] = []>>> numbers[1, 5]>>> #注:以上删除操作等价于del numbers[1:4]
2.3.3 列表方法

1.append:用于在列表末尾追加新的对象

2.count:统计某个元素在列表中出现的次数

3.extend:在列表的末尾一次性追加另一个序列中的多个值

4.index:从列表中找出某个值第一个匹配项的索引位置

5.insert:将对象插入列表中

6.pop:移除列表中的一个元素(默认是最后一个),并且返回该元素的值

7.remove:移除列表中某个值的第一个匹配项

8.reverse:将列表中的元素反向存放

9.sort:原位置对列表进行排序

10.高级排序

>>> #2.3.3 列表方法>>> #方法:一个与某些对象有紧密联系的函数,对象可能是列表、数字,字符串等>>> #方法调用:对象.方法(参数)>>> #1.append:用于在列表末尾追加新的对象>>> lst = [1, 2, 3]>>> lst.append(4)>>> list<type 'list'>>>> lst[1, 2, 3, 4]>>> #注:append方法只是在恰当位置修改原来的列表。即他不是简单地返回一个修改过的新列表,而是直接修改原来的列表。>>> #2.count:统计某个元素在列表中出现的次数>>> ['to','be','or','not','to','be'].count('to')2>>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]>>> x.count(1)2>>> x.count([1, 2])1>>> #3.extend:在列表的末尾一次性追加另一个序列中的多个值>>> a = [1, 2, 3]>>> b = [4, 5, 6]>>> a.extend(b)>>> a[1, 2, 3, 4, 5, 6]>>> #注:extend方法与连接操作的区别>>> #(1)extend方法修改了被拓展的序列(上例中,即a)>>> #(2)连接操作会创建一个包含a,b副本的新列表,然后返回这个列表>>> #(3)a=a+b比a.extend(b)效率低>>> #4.index:从列表中找出某个值第一个匹配项的索引位置>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']>>> knights.index('who')4>>> knights.index('herring')Traceback (most recent call last):  File "<pyshell#65>", line 1, in <module>    knights.index('herring')ValueError: 'herring' is not in list>>> #5.insert:将对象插入列表中>>> numbers = [1, 2, 3, 4, 5, 6, 7]>>> numbers.insert(3,'four')>>> numbers[1, 2, 3, 'four', 4, 5, 6, 7]>>> #6.pop:移除列表中的一个元素(默认是最后一个),并且返回该元素的值>>> x = [1, 2, 3]>>> x.pop()3>>> x[1, 2]>>> x.pop(0)1>>> x[2]>>> #注:可以使用append,pop方法实现栈>>> #7.remove:移除列表中某个值的第一个匹配项>>> x = ['to','be','or','not','to','be']>>> x.remove('be')>>> x['to', 'or', 'not', 'to', 'be']>>> x.remove('bee')Traceback (most recent call last):  File "<pyshell#81>", line 1, in <module>    x.remove('bee')ValueError: list.remove(x): x not in list>>> #8.reverse:将列表中的元素反向存放>>> x = [1 ,2 ,3]>>> list(reverse(x))Traceback (most recent call last):  File "<pyshell#84>", line 1, in <module>    list(reverse(x))NameError: name 'reverse' is not defined>>> list(reversed(x))[3, 2, 1]>>> #9.sort:原位置对列表进行排序>>> x = [4, 6, 2, 1, 7, 9]>>> x.sort();>>> x[1, 2, 4, 6, 7, 9]>>> #sort方法是在原位置进行排序。如果需要一个排好序的列表副本,同时又保留原有列表不变的时候,应该怎么做?>>> #正确做法1:先把x的副本赋值给y,然后对y进行排序>>> x = [4,6,2,1,7,9]>>> y = x[:]>>> y.sort()>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]>>> #正确做法2:使用sorted函数>>>  x = [4,6,2,1,7,9]   File "<pyshell#98>", line 1    x = [4,6,2,1,7,9]    ^IndentationError: unexpected indent>>>  x = [4,6,2,1,7,9]   File "<pyshell#99>", line 1    x = [4,6,2,1,7,9]    ^IndentationError: unexpected indent>>> x = [4,6,2,1,7,9]>>> y = sorted(x)>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]>>> #错误做法1:>>> x = [4,6,2,1,7,9]>>> y = x.sort()>>> y>>> print yNone>>> #错误原因:sort方法修改了x却返回了空值>>> #错误做法2:>>> y = x>>> y.sort()>>> x[1, 2, 4, 6, 7, 9]>>> y[1, 2, 4, 6, 7, 9]>>> #错误原因:y=x使x和y都指向同一个列表,同步修改>>> #10.高级排序>>> #(1)cmp自定义比较函数>>> #(2)key和reverse关键字参数>>> #根据元素的长度进行排序>>> x = ['aarevark','abalone','acmd','add','aerate']>>> x.sort(key=len)>>> x['add', 'acmd', 'aerate', 'abalone', 'aarevark']>>> #反向排序>>> x = [4,6,2,1,7,9]>>> x.sort(reverse=True)>>> x[9, 7, 6, 4, 2, 1]>>> 

0 0