读书笔记--《Python基础教程第二版》--第2章列表和元组

网友投稿 584 2022-09-21

读书笔记--《Python基础教程第二版》--第2章列表和元组

读书笔记--《Python基础教程第二版》--第2章列表和元组

第二章 列表和元组

2.1 序列的概览

序列:每个元素被分配一个序号

6种:列表、元组、字符串、Unicode字符串、buffer对象、xrange对象

序列可以嵌套序列

python容器的概念:序列、字典、集合

2.2 通用序列的操作

索引、分片、加、乘、属于、长度,最大值、最小值、迭代

2.2.1 索引

>>> greeting='Hello'

>>> greeting[0]

'H'

>>> greeting[-1]

'o'

>>> 'Hello'[1]

'e'

2.2.2 分片

>>> numbers=[1,2,3,4,5,6,7,8]

>>> numbers[3:6]  #第一个元素包含,第二个元素不包含

[4, 5, 6]

>>> numbers[0:1]

[1]

1、优雅的捷径

>>> numbers=[1,2,3,4,5,6,7,8,9,10]

>>> numbers[7:10]

[8, 9, 10]

>>> numbers[-3:-1]  #左边的元素一定要比右边的元素早出现

[8, 9]

>>> numbers[-3:]

[8, 9, 10]

>>> numbers[:3]

[1, 2, 3]

>>> numbers[:]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2、更长的步长

>>> numbers[0:10:1]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> numbers[0:10:2]

[1, 3, 5, 7, 9]

>>> numbers[::4]

[1, 5, 9]

步长为负数

>>> numbers[8:3:-1]

[9, 8, 7, 6, 5]

>>> numbers[10:0:-2]

[10, 8, 6, 4, 2]

>>> numbers[0:10:-2]

[]

>>> numbers[::-2]

[10, 8, 6, 4, 2]

>>> numbers[5::-2]

[6, 4, 2]

>>> numbers[:5:-2]

[10, 8]

>>>

2.2.3 序列相加

>>> [1,2,3]+[4,5,6]

[1, 2, 3, 4, 5, 6]

>>> 'hello. '+'world'

'hello. world'

>>> 'hello. '+[1,2,3]

Traceback (most recent call last):

File "", line 1, in

TypeError: cannot concatenate 'str' and 'list' objects

只有相同类型的序列才能进行连接操作

2.2.4 乘法

>>> 'python'*5

'pythonpythonpythonpythonpython'

>>> [42]*2

[42, 42]

None、空列表和初始化

>>> sequence=[None]*10

>>> sequence

[None, None, None, None, None, None, None, None, None, None]

2.2.5 成员资格

>>> users = ['mlh','foo','bar']

>>> raw_input('Enter your user name:') in users

Enter your user name:mlh

True

#字符串判断成员是,检测是字符,只要字符满足即可

>>> subject= '$$$ Get rich now!!! $$$'

>>> '$$$' in subject

True

>>> '$$' in subject

True

>>> '$' in subject

True

>>> '$$$$' in subject

False

>>> database = [['alert','1234'],['jones','9845']]

>>> if ['alert','1234'] in database:print 'Access granted'

...

Access granted

2.2.5 长度、最小值、最大值

>>> numbers=[100,34,478]

>>> len(numbers)

3

>>> max(numbers)

478

>>> min(numbers)

34

>>> max(2,3)

3

>>> min(9,3,2,5)

2

2.3 列表:python的苦力

列表不同于元组和字符串的地方:列表是可变的

2.3.1 list函数  list函数适用于所有类型的序列,不只是字符串

>>> list('Hello')

['H', 'e', 'l', 'l', 'o']

2.3.2 基本的列表操作

改变列表的方法:

1、改变列表:元素赋值,不能为一个位置不存在的元素进行赋值

>>> x=[1,1,1]

>>> x[1]=2

>>> x

[1, 2, 1]

2、删除元素 ,除了删除列表,还可以删除字典或其他变量

>>> del x[1]

>>> x

[1, 1]

3、分片赋值,可以加上步长,步长也可以为负数

>>> name=list('Perl')

>>> name

['P', 'e', 'r', 'l']

>>> name[2:]=list('ar')

>>> name

['P', 'e', 'a', 'r']

应用:可以进行不等长替换

>>> name=list('Perl')

>>> name[1:]=list('ython')

>>> name

['P', 'y', 't', 'h', 'o', 'n']

可以在一个位置上插入新值

>>> numbers=[1,5]

>>> numbers[1:1]=[2,3,4]

>>> numbers

[1, 2, 3, 4, 5]

可以用来删除元素

>>> numbers[1:4]=[]

>>> numbers

[1, 5]

等效于:del numbers[1:4]

2.3.3 列表方法

方法的调用:对象.方法(参数)

1、append 不返回值,改变的是原来的列表

>>> lst=[1,2,3]

>>> lst.append(4)

>>> lst

[1, 2, 3, 4]

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]

>>> a=[1,2,3]

>>> b=[4,5,6]

>>> a+b

[1, 2, 3, 4, 5, 6]

>>> a

[1, 2, 3]

>>> a=a+b  #并没有修改原来的列表

>>> a

[1, 2, 3, 4, 5, 6]

用分片复制实现同样的效果,但是可能性差

>>> a=[1,2,3]

>>> b=[4,5,6]

>>> a[len(a):]=b

>>> a

[1, 2, 3, 4, 5, 6]

4、index

index方法用于从列表中找出某个值第一个匹配项的索引位置

>>> knights=['We','are','the','knights','who','say','ni','who']

>>> knights.index('who')

4

>>> knights[4]

'who'

>>> knights.index('jack')

Traceback (most recent call last):

File "", line 1, in

ValueError: list.index(x): x not in list

5、insert

insert方法用于将对象插到列表中

>>> numbers=[1,2,3,5,6,7]

>>> numbers.insert(3,'four')

>>> numbers

[1, 2, 3, 'four', 5, 6, 7]

利用分片赋值实现,但是可读取差

>>> numbers=[1,2,3,5,6,7]

>>> numbers[3:3]=['four']

>>> numbers

[1, 2, 3, 'four', 5, 6, 7]

6、pop

pop方法会移除列表中的指定的一个元素(默认是最后一个),并且返回该元素的值

>>> x=[1,2,3]

>>> x.pop()

3

>>> x

[1, 2]

>>> x.pop(0)

1

>>> x

[2]

后进先出的数据结构:栈

append()+pop()

先进先出的数据结构:队列

insert(0,...)+pop() 总是加在最前面

>>> x=[1,2,3]

>>> x.append(x.pop())

>>> x

[1, 2, 3]

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 "", line 1, in

ValueError: list.remove(x): x not in list

8、reverse 不返回值 改变原列表,将原列表反向

>>> x=[1,2,3]

>>> x.reverse()

>>> x

[3, 2, 1]

>>> x=[1,2,3]

>>> list(reversed(x))

[3, 2, 1]

>>> reversed(x)

9、sort 没有返回值

sort方法用于在原位置对列表进行排序,改变原列表

>>> x=[4,6,2,1,7,9]

>>> x.sort()

>>> x

[1, 2, 4, 6, 7, 9]

用户需要排好序的副本,并不需要改变原列表

正确的做法:

>>> x=[4,6,2,1,7,9]

>>> y=x[:]

>>> y.sort()

>>> y

[1, 2, 4, 6, 7, 9]

>>> x

[4, 6, 2, 1, 7, 9]

>>> x=[4,6,2,1,7,9]

>>> y=sorted(x)

>>> y

[1, 2, 4, 6, 7, 9]

>>> x

[4, 6, 2, 1, 7, 9]

错误的做法:

>>> x=[4,6,2,1,7,9]

>>> y=x

>>> y.sort()

>>> x

[1, 2, 4, 6, 7, 9]

>>> y

[1, 2, 4, 6, 7, 9]

10、高级排序

>>> cmp(1,2)

-1

>>> cmp(2,1)

1

>>> cmp(1,1)

0

>>> numbers=[5,2,9,7]

>>> numbers.sort(cmp)

>>> numbers

[2, 5, 7, 9]

>>> x=[4,6,2,1,7

指定排序的key

>>> x=['aardvark','abalone','acme','add','aerate']

>>> x.sort(key=len)

>>> x

['add', 'acme', 'aerate', 'abalone', 'aardvark']

指定反向排序

>>> x=[4,6,2,1,7,9]

>>> x.sort(reverse=True)

>>> x

[9, 7, 6, 4, 2, 1]

2.4:元组:不可变序列

>>> 1,2,3

(1, 2, 3)

>>> (1,2,3)

(1, 2, 3)

>>> ()

()

如何实现一个值的元组呢?必须加一个读号

>>> (41)

41

>>> (41,)

(41,)

>>> 41

41

>>> 41,

(41,)

>>> 3*(42)

126

>>> 3*(40+2,)

(42, 42, 42)

2.4:tuple元素

将序列转换为元组

>>> tuple([1,2,3])

(1, 2, 3)

>>> tuple('abc')

('a', 'b', 'c')

>>> tuple((1,2,3))

(1, 2, 3)

2.4.2 基本元组的操作

创建元组

>>> x=1,2,3

>>> x[1]

2

访问元组

>>> x[0:2]

(1, 2)

2.4.3 元组的意义何在

1、元组可以在映射中当作键使用-而列表在不行

2、元组作为很多内建函数和方法的返回值存在

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:读书笔记--《Python基础教程第二版》-- 第三章 使用字符串
下一篇:Debian、Ubuntu系统中开机启动设置
相关文章

 发表评论

暂时没有评论,来抢沙发吧~