注:该笔记非原创,只是上网课记的
内容概要
目标:掌握列表和元组数据类型的各种操作(知识点应用案例)
·list,列表类型,用于存储一些数据的容器(有序&可修改)。{80%}
·tuple,元组类型,用于存储一些数据的容器(有序&不可修改)
字符串、列表、元组都是序列类型
列表
列表(list),是一个有序且可变的容器,在里面可以存放多个不同类型的元素。并且所放的元素可以修改。(先放的永远是在后放的前面)
定义
列表的定义是大括号 括起来,里边每个元素都用逗号隔开。
例如:
1 2 3
| user_list = ["苍老师","有坂深雪","大桥未久"] 字符串 单一类型 number_list = [98,88,666,12,-1] 整型单一类型 data_list = [1,True,"Alex","宝强","贾乃亮"] 多个类型
|
可变性:
1 2 3 4 5
| user_list = [] user_list.append("铁锤") user_list.append(123) user_list.append(True) print(user_list)
|
不可变类型:字符串、布尔、整型(已最小,内部数据无法进行修改)(在Python中创建,就已经是最小的单位了不能对他进行修改了)
可变类型:列表(内部数据元素可以修改)
独有功能
Python中为所有的列表类型的数据提供了一批独有的功能。
在开始学习列表之前,先来做一个字符串和列表的对比
- 字符串,不可变,即:创建好之后内部就无法修改。【独有功能都是新创建一份数据】
1 2 3 4
| name = "alex" data = name.upper() print(name) print(data)
|
- 列表,可变,即:创建号之后内部元素可以修改。【独有功能基本上都是直接操作列表内部,不会创建新的一份数据】
1 2 3 4
| user_list = ["车子","妹子"] user_list.append("嫂子")
print(user_list)
|
列表追加
追加,在原列表中尾部追加值
1 2 3 4 5 6
| data_list = [] v1 = input("请输入姓名") v2 = input("请输入姓名") data_list.append(v1) data_list.append(v2) print(data_list)
|
1 2 3 4 5 6 7 8 9 10
| user_list = []
while True: user = input("请输入用户名(Q退出)") if user == "Q": break user_list.append(user)
print(user_list)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| welcome = "欢迎使用NB游戏".center(30,"*") print(welcome)
user_count = 0 while True: count = input("请输入游戏人数:") if count.isdecimal(): user_count = int(count) break else: print("输入格式错误,人数必须是数字。")
message = "{}人参加游戏NB游戏。".format(user_count)
user_name_list = [] for i in range(1,user_count+1): tips = "请输入玩家姓名({}/{}):".format(i,user_count) name = input(tips) user_name_list.append(name)
print(user_name_list)
|
列表批量追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| tools = ["搬砖","菜刀","榔头"] weapon = ["AK47","M6"] tools.extend(weapon) 注意先后顺序
print(tools)
tools = ["搬砖","菜刀","榔头"] weapon = ["AK47","M6"] for item in weapon: tools.append(item)
print(tools) 其实extend的内部程序其实就是该程序
|
批量追加的两种方法
列表插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| user_list = ["苍老师","有坂深雪","大桥未久"] user_list.insert(0,"马蓉") user_list.insert(2,"李小璐") print(user_list)
name_list = [] while True: name = input("请输入购买火车票用户姓名(Q/q退出):") if name.upper() == "Q": break if name.startswith("刁"): name_list.insert(0,name) else: name_list.append(name) print(name_list)
|
列表根据值删除
(从左到右找到第一个删除)【慎用,里面没有就会报错】
1 2 3
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"] user_list.remove("Alex") print(user_list)
|
- 如果要删除多次:要进行循环
- 如果想要解决报错问题,可以先判断一下这个字符串存在不存在这个列表,再进行删除
1 2 3 4 5 6 7
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"] while True: if "Alex" in user_list: user_list.remove("Alex") else: break print(user_list)
|
1 2 3 4 5 6 7 8 9 10
| import random data_list = ["iphone12","二手充气女友","大保健一次","泰国5日游","避孕套"]
while True: name = input("自动抽奖程序,请输入自己的姓名:") value = random.choice(data_list) print("恭喜{},抽中{}。".format(name,value)) data_list.remove(value)
|
列表根据索引踢出元素
1 2 3 4 5 6 7 8 9 10 11 12 13
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
user_list.pop(1) print(user_list)
user_list.pop() print(user_list)
item = user_list.pop(1)
print(item) print(user_list)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| user_queue = []
while True: name = input("北京~上海火车票,购买请输入姓名排队(Q退出):") if name == "Q": break user_queue.append(name) tick_count = 3 for i in range(tick_count): username = user_queue.pop(0) message = "恭喜{},购买火车票成功。".format(username) print(message)
faild_user = "、".join(user_queue)
faild_message = "非常抱歉,票已售完,以下几位用户请选择其他出行方式,名单:{}。".format(faild_user) print(faild_message)
|
列表清空
1 2 3
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"] user_list.clear() print(user_list)
|
列表根据值获取索引
1 2 3 4 5
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
index = user_list.index("Alex") print(index) print(user_list)
|
1 2 3 4 5 6 7 8
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
if "Alex" in user_list: index = user_list.index("Alex") print(index) else: print("不存在") print(user_list)
|
列表元素排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| num_list = [11,22,4,5,11,99,88] print(num_list) num_list.sort() print(num_list)
num_list.sort(reverse=True)
print(num_list)
user_list = ["王宝强","ab陈羽凡","Alex","贾乃亮","贾乃","1"]
user_list.sort()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| print(user_list)
data = "1"
v1 = ord("王") print(v1) print(hex(v1)) print(bin(v1))
for char in data: v1 = ord(char) print(v1)
data_list = [] for char in data: v1 = ord(char) data_list.append(v1) print(data) print(data_list)
|
反转原列表
1 2 3 4
| user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"] user_list.reverse()
print(user_list)
|
公共功能
列表相加
1 2 3 4 5 6 7 8
| data = ["赵四","刘能"] + ["宋晓锋","范德彪"]
print(data)
v1 = ["赵四","刘能"] v2 = ["宋晓锋","范德彪"] v3 = v1 + v2 print(v3)
|
列表相乘
- 列表*整型:将列表中的元素再创建N份并生成一个新的列表
1 2 3 4 5 6 7
| data = ["赵四","刘能"] * 2 print(data)
v1 = ["赵四","刘能"] v2 = v1 * 2 print(v1) print(v2)
|
运算符in包含
- 由于列表内部是由多个元素组成,可以通过in来判断元素是否再列表中。
1 2 3 4 5 6 7 8 9 10
| user_list = ["狗子","二蛋","沙雕","alex"] result = "alex" in user_list
print(result)
if "alex" in user_list: print("在,把他删除") user_list.remove("alex") else: print("不在")
|
注意:该功能的返回值是布尔类型,一般是用来判断的
补充:字符串类型的也可以用in判断在不在里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| text = "打倒小日本" data = "日" in text
user_list = ["狗子","二蛋","沙雕","alex"] if "alex" in user_list: print("在,把他删除") user_list.remove("alex") else: print("不在")
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"] if "Alex" in user_list: index = user_list.index("Alex") user_list.pop(index)
text = input("请输入文本内容:")
forbidden_list = ["草","欧美","日韩"] for item in forbidden_list: text = text.replace(item,"**") print(text)
|
- 注意:列表检查元素是否存在时,是否采用逐一比较的方式,效率会比较低
索取长度
1 2
| user_list = ["范德彪","刘华强","尼古拉斯赵四"] print(len(user_list))
|
列表索引
一个元素的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| user_list = ["范德彪","刘华强","尼古拉斯赵四"] print(user_list[0]) print(user_list[2]) print(user_list[3])
user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[0] = "武沛齐" print(user_list)
user_list = ["范德彪","刘华强","尼古拉斯赵四"] del user_list[1]
user_list.remove("刘华强") ele = user_list.pop(1)
|
列表切片
1 2 3 4 5 6
| user_list = ["范德彪","刘华强","尼古拉斯赵四"]
print(user_list[0:2]) print(user_list[1:]) print(user_list[:-1])
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[0:2] = [11,22,33,44] print(user_list)
user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[2:] = [11,22,33,44] print(user_list)
user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[30000:] = [11,22,33,44] print(user_list)
user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[10000:] = [11,22,33,44] print(user_list) user_list = ["范德彪","刘华强","尼古拉斯赵四"] user_list[-10000:1] = [11,22,33,44] print(user_list)
user_list = user_list = ["范德彪","刘华强","尼古拉斯赵四"] del user_list[1:] print(user_list)
|
列表步长
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| user_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"]
print(user_list[1:4:2]) print(user_list[0::2]) print(user_list[1::2]) print(user_list[4:1:-1])
user_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"] new_data = user_list[::-1] print(new_data)
data_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"] data_list.reverse() print(data_list)
|
- 注意:revers只是将原来的列表调换数据,而第一个列表的翻转是创建一个新列表
1 2 3 4 5
| name = "武沛齐" name[::-1] data = name.reverse print(data)
|
列表for循环
1 2 3 4 5 6 7 8 9 10
| data_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"] for item in user_list: print(item)
user_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"]
for index in range(5):
item = user_list[index] print(item)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
user_list = ["刘的话","范德彪","刘华强","刘尼古拉斯赵四","宋小宝","刘能"] for item in user_list:
if item.startswith("刘"): user_list.remove(item) print(user_list)
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| user_list = ["刘的话","范德彪","刘华强","刘尼古拉斯赵四","宋小宝","刘能"] for index in range(len(user_list)-1,-1,-1):
item = user_list[index] if item.startswith("刘"): user_list.remove(item) print(user_list)
|
转换
1 2 3 4
| name = "武沛齐"
data = list(name) print(data)
|
1 2 3 4 5 6 7 8 9 10 11
| v1 = (11,22,33,44) vv1 = list(v1) print(vv1)
v2 = {"alex","eric","dsb"} vv2 = list(v2) print(vv2)
|
嵌套
列表属于容器,内部可以存放各种数据,所以他也支持列表的嵌套,如:
1 2 3 4
| data = ["谢广坤",["海燕","赵本山"],True,[11,22,33,44],"宋小宝"]
|
- 对于嵌套的值,可以根据之前学习的索引知识点来进行学习,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| data = ["谢广坤",["海燕","赵本山"],True,[11,22,33,44],"宋小宝"]
print(data[0]) print(data[1]) print(data[0][2])
print(data[1][-1]) data.append(666) print(data)
del data[-2] print(data)
data[1].append("谢大脚") print(data)
data[-2][1] = "alex" print(data)
data[1][0:2] = [999,666] print(data)
user_list = [] while True: user = input("请输入用户名:") pwd = input("请输入密码:")
data = [] data.append(user) data.append(pwd)
user_list.append(data)
user_list = [] while True: user = input("请输入用户名(Q退出):") if user == "Q": break pwd = input("请输入密码:") data = [user,pwd] use_list.append(data)
print(user_list)
|
元组
列表(list),是一个有序且可变的容器,在里面可以存放多个不同类型的元素。
元组(tuple),是一个有序且不可变的容器,在里面可以存放多个不同类型的元素。
如何体现不变呢?
记住一句话:《“我儿子永远不能换成别人,但是我儿子可以长大“》
定义
1 2 3 4 5 6 7 8
| v1 = (11,22,33) v2 = ("李杰","Alex") v3 = (True,123,"Alex",[11,22,33])
|
- 注意:建议在元组的最后多加一个逗号,用于标识他是一个元组
1 2
| 建议:建议在与那组的最后多加一个逗号 v3 = ("李杰","Alex",)
|
1 2 3 4 5 6 7 8 9 10
| d1 = (1)
d2 = (1,)
1.比较值v1 = (1) 和v2 = 1 和v3 = (1,),有什么区别? 2.比较值v1 =((1),(2),(3))和 v2 = ((1,)(2,)(3,),)有什么区别?
|
我的答案:
(1) v1表示整型,v2表示整型,v3表示元组,v1与v2没什么区别
(2) v1表示元组,里面的元素是整型,v2也表示元组,里面的元素表示元组
标准答案:
(1) v1整型,v2整形,v3元组
(2) v1元组,里面元素是1、2、3
v2元组,里面元素是元组(1)、(2)、(3)
独有功能
无
公共功能
元组相加
1 2 3 4 5 6
| data = ("赵四","刘能",) + ("宋晓锋","范德彪",) print(data) v1 = ("赵四","刘能",) v2 = ("宋晓锋","范德彪",) v3 = v1 + v2 print(v3)
|
元组相乘
- 相乘,列表*整型 将列表中的元素再创建N份并生成一个新的列表
1 2 3 4 5 6 7
| data = ("赵四","刘能",)*2 print(data)
v1 = ("赵四","刘能",) v2 = v1 * 2 print(v1) print(v2)
|
获取长度
1 2
| user_list = ("范德彪","刘华强","尼古拉斯赵四",) print(len(user_list))
|
元组索引
1 2 3 4
| user_list = ("范德彪","刘华强","尼古拉斯赵四",) print(user_list[0]) print(user_list[2]) print(user_list[3])
|
元组切片
1 2 3 4
| user_list = ("范德彪","刘华强","尼古拉斯赵四",) print(user_list[0:2]) print(user_list[1:]) print(user_list[:-1])
|
元组步长
1 2 3 4 5
| user_list = ("范德彪","刘华强","尼古拉斯赵四",) print(user_list[1:4:2]) print(user_list[0:2:2]) print(user_list[1::2]) print(user_list[4:1:-1])
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
user_list = ("范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能")
data = user_list[::-1]
user_list = ["范德彪","刘华强","尼古拉斯赵四","宋小宝","刘能"] data = user_list[::-1]
user_list.reverse() print(user_list)
|
元组for循环
1 2 3 4 5 6 7 8 9
| user_list = ("范德彪","刘华强","尼古拉斯赵四",) for item in user_list: print(item)
user_list = ("范德彪","刘华强","尼古拉斯赵四",) for item in user_list: if item == '刘华强': continue print()
|
- 目前:只有str、list、tuple可以被for循环。“xxx” [11,22,33 ] (111,22,33)
1 2 3
| for index in range(len(user_list)): item = user_list[index] print(item)
|
转换
- 其他类型转换为元组,使用tuple(其他类型),目前只有字符串和列表可以转换为元组
1 2 3 4 5 6 7
| name = "武沛齐" data = tuple(name) print(data)
name = ["武沛齐",18,"pythonav"] data = tuple(name) print(data)
|
嵌套
- 由于元组和列表都可以充当容器,他们内部可以放很多元素,并且支持元素内的各种嵌套。
1 2 3 4 5 6 7 8 9 10
| tu = ('今天姐姐不在家','姐夫和小姨子再客厅聊天',('姐夫问小姨子税后多少钱','小姨子低声说道说和姐夫还钱',)) tu1 = tu[0] tu2 = tu[1] tu3 = tu[2] tu4 = tu[2][1]
print(tu1) print(tu2) print(tu3) print(tu4)
|