列表
文章目录
1、概念
列表如同字符串类型一样,属于有序序列类型,每个元素按照顺序编号存取的,就像其他程序设计语言中的数组,但数组通常存储相同数据类型的元素,而列表可以存储不同类型的元素,并且长度是可变的。(由一系列变量组成的可变序列容器)
2、列表常见操作
2.1列表的创建
(1)通过[]建立列表
通过“[]”内不带数据的方式将产生空列表;
lst = [] #创建一个空列表
lst = ["张三", "李四", "王五"] #创建一个带数据的列表
(2)通过list建立列表
lst = list() # 创建一个空列表
lst = list("hello") #将其他序列转换为列表,()中必须是序列
2.2添加元素
(1)追加(在末尾追加)
lst.append("王二")
(2)插入(在指定位置添加)
lst.insert(1,True)
2.3删除元素
(1)根据元素删除
lst.remove("王二")
(2)根据位置删除
del lst[0]
2.4列表遍历
(1)索引方式
索引方式采用for语句配合range函数完成,range函数根据列表的长度生成所有索引值,其格式:
"""
for i in range(len(L)):
对L(i)进行处理
"""
lst = list("hello")
for i in range(len(lst)):
print(lst[i])
(2)枚举方式
枚举方式指将列表中的每项直接取出进行处理,其格式:
"""
for item in L:
对item进行处理
"""
lst = list("hello")
for item in lst:
print(item)
2.5 定位元素,可以进行增删改查
(1)切片–删除
lst = list("hello")
del lst[0:2]
print(lst)
(2)切片–修改
lst = list("hello")
lst[1:2] = ['a', 'b']
print(lst)
通过切片将列表拿出,会重新创建新列表
3、内存
列表的内存图好比是c语言中的指针数组
练习
"""
练习1:在控制台录入,西游记中你喜欢的人物,输入空字符串,打印(一行一个)所有人物:
"""
nameList = []
while True:
name = input('输入你最喜欢的人物:')
if name == " ":
for item in nameList:
print(item)
else:
nameList.append(name)
"""
练习2:在控制台中录入,所有学生成绩
输入空字符串,打印(一行一个)所有成绩
打印最高分,打印最低分,打印平均分
"""
scoreList = []
while True:
score = input("输入学生成绩")
if score == "":
for item in scoreList:
print(item)
print("Max = %d" % max(scoreList))
print("min = %d" % min(scoreList))
print("avg = %d" % (sum(scoreList)/len(scoreList)));
else:
scoreList.append(int(score))
"""
练习3:在控制台中录入,所有学生姓名
如果姓名重复,则提示”姓名已经存在“,不添加到列表中
如果录入空字符串,则倒序打印所有学生
"""
studentName = []
while True:
name = input("输入学生姓名")
if name == "":
break
elif name in studentName:
print("学生已经存在!\n")
break
else:
studentName.append(name)
for item in studentName[::-1]:
print(item)
4列表内存图
list01 = ["张无忌", "赵敏"]
list02 = list01
list01 = ["张无忌", "赵敏"]
list02 = list01
list01[0] = "无忌"
print(list02)
"""
列表内存图
"""
list01 = ["张无忌", "赵敏"]
list02 = list01
list01 = ["无忌"]
print(list02)
"""
列表内存图
"""
list01 = [800, 1000]
list02 = list01[:]
list02[0] = 900
print(list01[0])
"""
列表内存图
"""
list01 = [800, [1000, 500]]
list02 = list01
list01[1][0] = 900
print(list02[1][0])
"""
列表内存图
"""
list01 = [800, [1000, 500]]
list02 = list01[:]
list01[1][0] = 900
print(list02[1][0])
5浅拷贝和深拷贝
浅拷贝:复制过程中,只复制一层变量,不会复制深层变量绑定的数据(默认的就是浅拷贝)
深拷贝:复制整个依赖的变量(深拷贝浪费内存)
"""
深拷贝内存图
"""
import copy
list01 = [800, [1000, 500]]
list02 = copy.deepcopy(list01)
list01[1][0] = 900
print(list02[1][0])
6列表推导式
(1)概念
列表推导式又称为列表解析式,是一种简洁的快速对列表进行遍历、过滤进而达到重组的方法。列表推导式在逻辑商等价于一个循环语句,只是形式更简单
使用简易方法,将可迭代对象转换为列表
(2)格式
[结果表达式 for x in 序列 1 [… for xn in 序列 n if 条件表达式]]
变量 = [表达式 for 变量 in 可迭代对象]
变量 = [表达式 for 变量 in 可迭代对象 if 条件]
(3)作用
对序列对象的元素进行遍历、过滤,并以“结果表达式”作为元素形成新的序列。条件表达式可以省略,序列可以是1个或者多个嵌套。
序列一般是列表、元素、集合、和字典,若是字符串作为整体粗粒,即仅有一个元素
注意:最外层[]表示是列表
"""
列表推导式
"""
# 将list01中所有元素,增加1以后存入list02中
list01 = [1,2,3,4,5,6]
list02 = [item+1 for item in list01]
print(list02)
"""
列表推导式
"""
# 将list01中大于10的元素,增加1以后存入list02中
list01 = [1, 2, 3, 4, 5, 6, 8, 10, 11]
list02 = [item+1 for item in list01 if item > 10]
print(list02)
"""
列表推导式
"""
# 使用range生成1-10之间的数字,将数字的平方存入list01中
list01 = [x * x for x in range(1, 11)]
print(list01)
# 将list01中所有的奇数存入list02
list02 = [item for item in list01 if item % 2 == 1]
print(list02)
# 将list01中所有的偶数存入list03
list03 = [item for item in list01 if item % 2 == 0]
print(list03)
# 将list01中所有偶数大于5的数字增加1后存入list04
list04 = [item+1 for item in list01 if item % 2 == 0 and item > 5]
print(list04)