文章目录
1. 列表简介
- 列表是由一系列按特定顺序排列的元素组成,一般给列表指定一个表示复数的名称(如bicycles,names等)
- 简单列表示例:
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
1.1 访问列表元素
- 访问列表元素:指出列表名称,再指出元素索引,索引放在[]内
- 第一个列表元素索引为0,第二个是1,依次类推
- 快速访问最后一个列表元素,将其索引指定为-1,倒数第二个指定为-2,依次类推
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> print(bicycles[0])
trek
>>> print(bicycles[1])
cannondale
>>> print(bicycles[-1])
specialized
>>> print(bicycles[-2])
redline
1.2 使用列表各个值
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> message = "My first bicycle was a "+bicycles[0].title()+"."
>>> print(message)
My first bicycle was a Trek.
2. 列表操作(修改、添加、删除)
2.1 修改
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles[0]='Giant'
>>> print(bicycles)
['Giant', 'cannondale', 'redline', 'specialized']
2.2 添加
2.2.1 列表末尾添加
列表名.append(‘添加元素名’)
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.append('Giant')
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized', 'Giant']
2.2.2 列表中添加
列表名.insert(索引号,‘添加元素名’)
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.insert(2,'Giant')
>>> print(bicycles)
['trek', 'cannondale', 'Giant', 'redline', 'specialized']
2.3 删除
2.3.1 del 删除
del 列表名[索引号]
使用del 语句将值从列表删除后,无法再访问
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> del bicycles[1]
>>> print(bicycles)
['trek', 'redline', 'specialized']
2.3.2 remove 删除
列表民.remove(元素名) #注意是圆括号
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.remove('cannondale')
>>> print(bicycles)
['trek', 'redline', 'specialized']
2.3.3 pop 弹出
- 列表名.pop() :删除列表末尾元素
- 列表名.pop(索引号):删除指定位置处元素
- pop删除的元素我们可以接着使用它,而del不行
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> popped_bicycles = bicycles.pop()
>>> print(bicycles)
['trek', 'cannondale', 'redline']
>>> print(popped_bicycles)
specialized
3. 组织列表
3.1 sort()
按字母顺序,对列表进行永久性排序
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.sort()
>>> print(bicycles)
['cannondale', 'redline', 'specialized', 'trek']
按字母顺序相反顺序排序
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.sort(reverse=True)#True不能写成true
>>> print(bicycles)
['trek', 'specialized', 'redline', 'cannondale']
>>>
3.2 sorted()
对列表进行临时性排序,并未实质性更改列表顺序,只是使用此函数后可以字母顺序展现
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> print(sorted(bicycles))
['cannondale', 'redline', 'specialized', 'trek']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
3.3 reverse()
对列表元素顺序倒着输出
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> bicycles.reverse()
>>> print(bicycles)
['specialized', 'redline', 'cannondale', 'trek']
3.4 len()
计算列表元素个数
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> len(bicycles)
4
4. 使用列表时的错误
当列表为空时,此时访问元素将报错
>>> bicycles = []
>>> print(bicycles[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range