其实,写这个是为了督促自己看书……然后 ……其实没有然后了,人一松懈下来,就……ε=(´ο`*)))唉
第四章 列表
①列表数据类型
……
列表是数据类型!!!
……
……
没啥好说的了,要不是看书,还以为和数组一样,现在看完全不一样啊,绝望.jpg
“列表”是一个值,它包括多个字构成的序列,术语“列表值”指的是列表本身(它作为一个值,可以保存在变量中,或传递给函数,像所有其他值一样),而不是指列表值之内的那些值。列表看起来像这样:['cat', 'bat', 'rat', 'elephant']。就像字符串值用引号来标记字符串起止一样,列表用左方括号(代表)开始,右方括号(代表)结束。列表中的值称为“表项”,用逗号隔开。
——摘自书59页(那两个代表是我为了语意连贯自己加的)
在交互环境中大家可以尝试输入
[1,2,3],输出:[1,2,3]。
['cat', 'bat', 'rat', 'elephant'],输出:['cat', 'bat', 'rat', 'elephant']
②用下标获取列表中的单个值
就是这里让我联想到数组的……同样,还是直接先举栗子吧ヾ(◍°∇°◍)ノ゙
- spam=['cat','bat','rat','elephant']
- print(spam[0])
- print(spam[1])
- print(spam[2])
- print(spam[3])
- print(spam[-1])
- print(spam[-3])
- print(spam[4])
从输出信息就可以看出,列表中的第一个表项(请允许我在后面称之为元素)对应的序号(请允许我强行取名)是0,也就是说,这个列表中有4个元素,但是元素的序号不是1、2、3、4而是0、1、2、3,而,一旦要访问第5个元素(实际上不存在),输入4的时候,会报错。这与C++中的数组也有相似之处。那么,如果是负数序号怎么办呢?那就从序号0的位置返回序号3的位置然后逆时针向后,也就是按3、2、1的顺序依次试-1、-2、-3.。
如上图所示(图画的不好,大家不要见怪)。
那么,如果是序号为4,也就是第5个元素呢?--》IndexError: list index out of range===》也就是超限
除了刻板的输出,当然,还有一些其他的操作,比如:
- spam=['cat','bat','rat','elephant']
- print('hello '+spam[0])
输出:hello cat另外再补充一点,序号不能是浮点数,比如:
- spam=['cat','bat','rat','elephant']
- print(spam[1.0])
报错信息:
Traceback (most recent call last):
File '/usercode/file.py', line 2, in <module>
print(spam[1.0])
TypeError: list indices must be integers, not float
列表也可以包含其他列表值。这些列表的列表中的值,可以通过多重下标来访问:
- spam=[['cat','bat','rat','elephant'],[10,20,30,40,50]]
- print(spam[0][1])
- print(spam[1][4])
输出:
bat
50
第一个下标表示使用哪个值,第二个下标表明该列表中哪一个值。
③利用切片获取子列表
spam[2]是一个列表和下标。
spam[1:4]是一个列表和切片。
在一个切片中,第一个整数是切片开始处的下标。第二个整数是切片结束处的下标。
- spam=['cat','bat','rat','elephant']
- print(spam[0:2])#output:['cat', 'bat']
- print(spam[-2:2])#output:[]
- print(spam[-1:0])#output:[]
- print(spam[0:0])#output:[]
- print(spam[0:4])#output:['cat', 'bat', 'rat', 'elephant']
- print(spam[0:5])#output:['cat', 'bat', 'rat', 'elephant']
④用len()获取列表长度
- spam=['cat','bat','rat','elephant']
- print(len(spam))#output:4
⑤列表的增删改查
1、增——用append()和insert()方法在列表中添加值&&列表连接与链表复制
- spam=['cat','bat']
- spam.insert(2,'rat')
- spam.append('elephant')
- print(spam)#output:['cat', 'bat', 'rat', 'elephant']
- spam=['cat','bat']+['rat', 'elephant']
- print(spam)#output:['cat', 'bat', 'rat', 'elephant']
- bacon=['cat','bat']+[1,2]
- print(bacon)#output:['cat', 'bat', 1, 2]
↑列表连接↓列表复制
- spam=['cat','bat']*3
- print(spam)#output:['cat', 'bat', 'cat', 'bat', 'cat', 'bat']
2、删——用del语句&&用remove()方法
- spam=['cat','bat','rat','elephant']
- del spam[2]
- print(spam)#output:['cat', 'bat', 'elephant']
- spam=['cat','bat','rat','elephant']
- spam.remove('elephant')
- print(spam)#output:['cat', 'bat', 'rat']
3、改——用下标改变列表中的值&&sort()排序
- spam=['cat','bat','raccoon','elephant']
- spam[2]='rat'
- print(spam)#output:['cat', 'bat', 'rat', 'elephant']
- spam=['cat','bat','raccoon','elephant']
- spam.sort()
- print(spam)#output:['bat', 'cat', 'elephant', 'raccoon']
- bacon=[61,1,4,3,2,45,-2]
- bacon.sort()
- print(bacon)#output:[-2, 1, 2, 3, 4, 45, 61]
4、查——in和not in操作符&&用index()方法
- spam=['cat','bat','raccoon','elephant']
- print('raccoon' in spam)#output:True
- print('raccoon' not in spam)#output:False
- print('rat' in spam)#output:False
- spam=['cat','bat','raccoon','elephant']
- print(spam.index('raccoon'))
- print(spam.index('rat'))
输出:
2
Traceback (most recent call last):
File '/usercode/file.py', line 3, in <module>
print(spam.index('rat'))#output:False
ValueError: 'rat' is not in list
如果所查找的值在列表中,就返回该值的下标,如果不在,Python就报ValueError
⑥使用列表
书里的例子是用来储存猫的名字(WA的一声哭了出来,也想养一只猫啊)
先是暴力版本:
- print('Enter the name of cat 1:')
- catName1=input()
- print('Enter the name of cat 2:')
- catName2=input()
- print('Enter the name of cat 3:')
- catName3=input()
- print('Enter the name of cat 4:')
- catName4=input()
- print('Enter the name of cat 5:')
- catName5=input()
- print('Enter the name of cat 6:')
- catName6=input()
- print('The cat names are:')
- print(catName1+' '+catName2+' '+catName3+' '+catName4+' '+catName5+' '+catName6)
这个……怎么说呢,乍一看没有毛病,但是真的好麻烦
然后就出现了升级改进版:
- catNames=[]
- while True:
- print('Enter the name of cat '+str(len(catName)+1)+'Or anter nothing to stop.):')
- name=input()
- if name=='':
- break
- catNames=catNames+[name]
- print('The cat names are:')
- for name in catNames:
- print(' '+name)
str(len(catName)+1)
这一句是计算列表中元素的个数,然后+1,再将整型转换为字符串(不然没法连接)
另外:
- for i in range(4):
- ↑↓效果相同
- for i in [0,1,2,3]:
⑦赋值——多重赋值&&增强的赋值操作
- cat=['fat', 'black', 'loud']
- size=cat[0]
- color=cat[1]
- disposition=cat[2]
- 可以直接写作
- cat=['fat', 'black', 'loud']
- size,color,disposition=cat
- #但是变量的个数必须与列表的长度严格一致,否则↓
- cat=['fat', 'black', 'loud']
- size,color,disposition,name=cat
- 报错信息如下:
- Traceback (most recent call last):
- File '/usercode/file.py', line 3, in <module>
- size,color,disposition,name=cat
- ValueError: need more than 3 values to unpack
增强的赋值语句 | 等价的赋值语句 |
spam+=1 | spam=spam+1 |
spam-=1 | spam=spam-1 |
spam*=1 | spam=spam*1 |
spam/=1 | spam=spam/1 |
spam%=1 | spam=spam%1 |
然后,我们可以这样:
- cat=['fat', 'black', 'loud']
- cat*=3
- print(cat)
- #output:['fat', 'black', 'loud', 'fat', 'black', 'loud', 'fat', 'black', 'loud']
⑧类似列表的类型:字符串和元组
列表并不是唯一表示序列值的数据类型。比如,字符串与列表实际上很相似。也可以用于列表类似的方法操作字符串:
- name='Zophie'
- print(name[0])
- print(name[-2])
- print(name[0:4])
- print('Zo' in name)
- for i in name:
- print(' * * * '+i+' * * * ')
输出:
Z
i
Zoph
True
* * * Z * * *
* * * o * * *
* * * p * * *
* * * h * * *
* * * i * * *
* * * e * * *
而元组除了两个方面,与列表几乎完全一样。
两方面:
1、元组输入用的是圆括号,而非方括号
- eggs=('Hello',42,0.5)
- print (eggs[0])
- print (eggs[1:3])
- print (len(eggs))
输出:
Hello
(42, 0.5)
3
2、但是元组与字符串一样,与列表不同的是,他们都是不可变的,不能改变他们的值。
比如:
- eggs=('Hello',42,0.5)
- eggs[1]=99
Traceback (most recent call last):
File '/usercode/file.py', line 2, in <module>
eggs[1]=99
TypeError: 'tuple' object does not support item assignment
好了,现在,讲到了一个关键问题
—— ——可变和不可变数据类型
安利别人写的博客(ε=(´ο`*)))唉我什么时候才能写这么好的博客)
⑨用list()和tuple()函数来转换类型
- >>>tuple(['cat', 'dog', 5])
- ('cat', 'dog', 5)
- >>>list(('cat', 'dog', 5))
- ['cat', 'dog', 5]
- >>>list('hello')
- ['h', 'e', 'l', 'l', 'o']
⑩引用
与C++中类似,相当于起一个别名。
一旦更改任何一个的内容,列表与列表的引用都会改变。
就像无论别人是直接叫我们的外号还是直接叫我们的名字我们都知道是是在叫我们
然后列表的引用直接是:列表的引用的名字=列表的名字,这样就可以了
举例:
- spam=['A','B','C','D']
- cheese=spam
- cheese[1]=42
- print(spam)
- print(cheese)
输出:
['A', 42, 'C', 'D']
['A', 42, 'C', 'D']
⑪copy模块的copy()和deepcopy()函数
- import copy
- spam=['A','B','C','D']
- cheese=copy.copy(spam)
- cheese[1]=42
- print(spam)
- print(cheese)
输出:
['A', 'B', 'C', 'D']
['A', 42, 'C', 'D']
copy()函数,相当于建立了一个新的链表,能独立于第一个链表修改
如果要复制的链表中包含了链表,那么使用deepcopy()函数可以用来同时复制内部的链表。
'''
好啦,就先到这里啦ヾ(◍°∇°◍)ノ゙
'''
联系客服