CSDN 课程推荐:《8小时Python零基础轻松入门》,讲师齐伟,苏州研途教育科技有限公司CTO,苏州大学应用统计专业硕士生指导委员会委员;已出版《跟老齐学Python:轻松入门》《跟老齐学Python:Django实战》、《跟老齐学Python:数据分析》和《Python大学实用教程》畅销图书。
目录
- 【2.1】列表是什么
- 【2.1.1】访问列表元素
- 【2.1.2】列表切片
- 【2.1.3】使用列表中的各个值
- 【2.1.4】修改元素
- 【2.1.5】添加元素
- 【2.1.6】删除元素
- 【2.1.7】使用方法 index() 查找指定元素位置
- 【2.1.8】使用方法 count() 统计指定元素数量
- 【2.1.9】清空列表
- 【2.2】组织列表
- 【2.2.1】使用方法 sort() 对列表进行永久排序
- 【2.2.2】使用函数 sorted() 对列表进行临时排序
- 【2.2.3】使用方法 reverse() 对列表进行反向排序
- 【2.2.4】确定列表的长度
- 【2.2.5】合并列表
【2.1】列表是什么
列表由一系列按特定顺序的元素组成,在 Python 中用方括号( [ ] )来表示列表,并用逗号来分隔其中的元素,例:
list1 = ['a','b','c','d','e','f']
list2 = ['abc', 'xyz', 2018, 2020]
list3 = [1, 2, 3, 4, 5 ,6]
list4 = ["a", "b", "c", "d"]
print(list1, list2, list3 ,list4)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['abc', 'xyz', 2018, 2020] [1, 2, 3, 4, 5, 6] ['a', 'b', 'c', 'd']
【2.1.1】访问列表元素
列表是有序集合,因此要访问列表的元素,只需要将该元素的位置或索引告诉Python即可,注意:在Python中的第一个列表元素的索引为0,而不是1
list = ['a','b','c','d','e','f']
print(list[0])
print(list[3])
print(list[-1]) #Python为访问最后一个列表元素提供了一种特殊语法,通过将索引指定为-1,可以让Python返回最后一个列表元素
print(list[-3])
输出结果如下:
a
d
f
d
【2.1.2】列表切片
list = ['a','b','c','d','e','f']
print(list[:]) #省略全部,代表截取全部内容,可以用来将一个列表拷给另一个列表
print(list[:3]) #省略起始位置的索引,默认起始位置从头开始,结束位置索引为2
print(list[3:]) #省略结束位置的索引,默认结束位置为最后一个,开始位置索引为3
print(list[1:4]) #开始位置索引为1,结束位置索引为3,顾头不顾尾
print(list[4:1]) #从左到右索引,因此为空值
print(list[-1:-3]) #从左到右索引,因此为空值
print(list[-3:-1]) #开始位置索引为倒数第三个,结束位置索引为倒数第二个
print(list[1:5:2]) #开始位置索引为1,结束位置索引为4,间隔2
print(list[5:1:-1]) #反向取值,开始位置索引为5,结束位置索引为2
print(list[::-1]) #反向取值,反向输出列表
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c']
['d', 'e', 'f']
['b', 'c', 'd']
[]
[]
['d', 'e']
['b', 'd']
['f', 'e', 'd', 'c']
['f', 'e', 'd', 'c', 'b', 'a']
【2.1.3】使用列表中的各个值
可像使用其他变量一样使用列表中的各个值,例如,我们可以使用拼接根据列表中的值来创建消息:
list = ['python', 'c', 'c++', 'java', 'php']
message = "My favorite language is " + list[0].title() + "!"
print(message)
输出结果如下:
My favorite language is Python!
【2.1.4】修改元素
修改列表元素的语法与访问列表元素的语法类似,要修改列表元素,可指定列表名和要修改的元素的索引,再次指定该元素的新值:
names = ['zhangsan', 'lishi', 'wanger', 'liming', 'xiaowang']
print(names)
names[1] = 'lifang'
print(names)
输出结果如下:
['zhangsan', 'lishi', 'wanger', 'liming', 'xiaowang']
['zhangsan', 'lifang', 'wanger', 'liming', 'xiaowang']
【2.1.5】添加元素
- 使用方法 append() 在列表末尾添加元素
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
list.append('g')
print(list)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
- 使用方法 insert() 在列表指定位置添加元素
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
list.insert(2,"h") #其中括号里的数字表示要插入的位置,此后面的元素将右移一个位置
print(list)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g']
【2.1.6】删除元素
- 使用 del 语句删除元素
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
del list[3]
print(list)
输出结果如下:
list = ['a', 'b', 'c', 'd', 'e', 'f']
list = ['a', 'b', 'c', 'e', 'f']
- 使用方法 pop() 删除最后一个元素
方法 pop() 可以删除列表末尾的元素,并让你能够接着使用它。术语弹出(pop)源自这样的类比:列表就像是一个栈,而删除列表末尾的元素就相当于弹出栈顶元素:
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
new_list = list.pop()
print(list)
print(new_list)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e']
f
- 使用方法 pop() 删除任意位置元素
可以使用 pop() 来删除列表中任何位置的元素,只需要在括号中指定要删除的元素的索引即可:
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
new_list = list.pop(1)
print(list)
print(new_list)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'c', 'd', 'e', 'f']
b
- 使用方法 remove() 删除未知位置元素
当我们不知道元素的位置,只知道元素的值的时候,就可以使用方法 remove()
list = ['a', 'b', 'c', 'd', 'e', 'f']
print(list)
list.remove('d')
print(list)
输出结果如下:
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'e', 'f']
【2.1.7】使用方法 index() 查找指定元素位置
list = ["a", "b", "c", "d", "e", "a"]
print(list.index('c'))
输出结果如下:
2
【2.1.8】使用方法 count() 统计指定元素数量
list = ["a", "b", "c", "d", "e", "a"]
print(list.count('a'))
输出结果如下:
2
【2.1.9】清空列表
list = ["a", "b", "c", "d", "e", "a"]
list.clear()
print(list)
输出结果如下:
[]
【2.2】组织列表
在创建的列表中,元素的排列顺序常常是无法预测的,因为我们并非总能控制用户提供数据的顺序。有时候,我们希望保留列表元素最初的排列顺序,而有时候又需要调整排列顺序。Python提供了很多组织列表的方式,可根据具体情况选用
【2.2.1】使用方法 sort() 对列表进行永久排序
使用方法 sort() 可以对列表按照特殊符号,数字,大写字母,小写字母顺序进行永久排序:
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
输出结果如下:
['audi', 'bmw', 'subaru', 'toyota']
还可以按与字母顺序相反的顺序排列列表元素,只需要向 sort() 方法传递参数 reverse = True 就可以了:
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse = True)
print(cars)
输出结果如下:
['toyota', 'subaru', 'bmw', 'audi']
【2.2.2】使用函数 sorted() 对列表进行临时排序
要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序:
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the sorted reverse list:")
print(sorted(cars, reverse=True))
print("\nHere is the original list again:")
print(cars)
输出结果如下:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']Here is the sorted reverse list:
['toyota', 'subaru', 'bmw', 'audi']Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
【2.2.3】使用方法 reverse() 对列表进行反向排序
要反转列表元素的排列顺序,可使用方法 reverse()
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.reverse()
print(cars)
输出结果如下:
['subaru', 'toyota', 'audi', 'bmw']
【2.2.4】确定列表的长度
使用函数 len() 可以快速获悉列表的长度:
>>>cars = ['bmw', 'audi', 'toyota', 'subaru']
>>>len(cars)
4
【2.2.5】合并列表
- 使用方法 extend() 合并列表
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list1.extend(list2) #将列表list2添加到list1当中去
print(list1)
print(list2)
输出结果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
- 使用 “+” 号合并列表
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
print(list1 + list2)
print(list2 + list1)
输出结果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 1, 2, 3, 4]
- 使用切片合并列表
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list1[len(list1) : len(list1)] = list2 #len(list1)代表要将list2插入list1中的位置
print(list1)list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list1[0 :0] = list2
print(list1)list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list1[1:1] = list2
print(list1)
输出结果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 1, 2, 3, 4]
[1, 'a', 'b', 'c', 'd', 2, 3, 4]