Python3笔记017 - 4.2 列表
第4章 序列的应用
- 4.1 序列
- 4.2 列表
- 4.3 元组
- 4.4 字典
- 4.5 集合
- 4.6 列表、元组、字典、集合的区别
python的数据类型分为:空类型、布尔类型、数字类型、字节类型、字符串类型、元组类型、列表类型、字典类型、集合类型
在python中序列是一块用于存放多个值的连续内存空间。
python内置了5种序列分别是元组、列表、字典、集合、字符串。
本章将介绍元组、列表、字典、集合四种序列类型,字符串将在第5章介绍
4.2 列表
列表的所有元素都放在一对中括号"[]"中,两个相邻元素间使用逗号","分隔,在内容上,可以将整数、实数、字符串、列表、元组等任何类型的内容放入到列表中,并且元素的类型可以不同,因为它们之间没有任何关系。
列表是可变序列
4.2.1 列表的创建和删除
1、直接使用[]创建列表
listname = [element1, element2, ...elementn]
参数说明
listname表示列表名称
element表示列表元素,个数没限制
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
print(list1)
output:['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
2、创建空列表
emptylist = []
3、通过list()创建数值列表
list(data)
参数说明
list是一个函数
data表示可以转换为列表的数据
list(range(10,20,2)) # 创建一个10~20之间(不包括20)所有偶数的列表
# 拓展:生成逆序序列
list(range(10,-1,-1)) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
4、删除列表
对于已经创建的列表,不再使用时,可以用del语句将其删除。前提是列表已存在。在python中,del并不常用,因为python自带垃圾回收机制,会自动销毁不用的列表。
del listname
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
print(list1)
del list1
print(list1)
output:
['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
NameError: name 'list1' is not defined
4.2.2 访问列表元素
1、通过索引访问
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
print(list1[0])
print(list1[1])
output:
Monday
Tuesday
4.2.3 遍历列表
1、使用for循环实现
for item in listname:
# 输出item
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
for i in list1:
print(i)
output:
Monday
Tuesday
wednesday
Thursday
Friday
Saturday
Sunday
2、使用for循环和enumerate()函数实现
for index, item in enumerate(listname):
# 输出index和item
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
for index, item in enumerate(listname):
print(index,item)
output:
0 Monday
1 Tuesday
2 wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
4.2.4 添加、修改、删除列表元素
1、添加元素
listname.append(obj)
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
list1.append("Sunday")
print(list1)
output:
['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Sunday']
2、修改元素
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sundayy"]
list1[-1] = "Sunday"
print(list1)
output:
['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
3、删除元素
# 根据值来删除元素
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
list1.remove("Sunday")
print(list1)
output:
['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday']
# 根据索引来删除元素
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
del list1[-1]
print(list1)
output:
['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday']
4.2.5 对列表进行统计和计算
1、获取指定元素出现的次数
listname.count(obj)
list1 = ["Monday","Monday","Monday","Thursday","Friday","Saturday","Sunday"]
print(list1.count("Monday"))
output:
3
2、获取指定元素首次出现的下标
listname.index(obj)
list1 = ["Monday","Tuesday","wednesday","Thursday","Friday","Saturday","Sunday"]
print(list1.index("Monday"))
print(list1.index("Sunday"))
output:
0
6
3、统计数值列表的元素和
sum(iterable[,start])
list1 = list(range(1,10,1))
print(sum(list1))
output:
45
4.2.6 对列表进行排序
1、使用列表对象的sort()方法
改变原序列的顺序
listname.sort(key=None, reverse=False)
list1 = [4, 6, 2, 9]
list1.sort()
print(list1)
output:
[2, 4, 6, 9]
2、使用内置的sorted()函数实现
不会改变原序列的顺序
sorted(iterable,key=None,reverse=False)
list1 = [4, 6, 2, 9]
print(sorted(list1))
output:
[2, 4, 6, 9]
a = [1, 4, 2, 3, 1]
print(sorted(a, reverse=True))
a = [{'name': 'xiaoming', 'age': 18, 'gender': 'male'}, {'name': 'xiaohong', 'age': 20, 'gender': 'female'}]
print(sorted(a, key=lambda x: x['age'], reverse=False))
output:
[4, 3, 2, 1, 1]
[{'name': 'xiaoming', 'age': 18, 'gender': 'male'}, {'name': 'xiaohong', 'age': 20, 'gender': 'female'}]
3、列表反转
def reverse(lst):
return lst[::-1]
r = reverse([1, -2, 3, 4, 1, 2])
print(r) # [2, 1, 4, 3, -2, 1]
4.2.7 列表推导式
list = [Expression for var in range]
参数说明
list表示生成的列表名称
Expression表达式,用于计算新列表的元素
var循环变量
range采用这个函数生成的range对象
敬请关注个人微信公众号:测试工匠麻辣烫
敬请关注个人微信公众号:测试工匠麻辣烫