Python基础语法文章导航:
- Python基础(01初识数据类型&变量)
- Python基础(02条件&循环语句)
- Python基础(03字符串格式化&运算符&进制&编码)
- Python基础(04 基础练习题)
- Python数据类型(day05整型&布尔类型&字符串类型)
目录
一.列表
列表(list),是一个有序且可变的容器,在里面可以存放多个不同类型的元素。
1.定义
data_list = [1,True,"Alex","宝强","贾乃亮"]
2.独有功能
先来做一个字符串和列表的对比:
字符串,不可变,即:创建好之后内部就无法修改。【独有功能都是新创建一份数据】。
name = "alex"
data = name.upper()
print(name)
print(data)
列表,可变,即:创建好之后内部元素可以修改。【独有功能基本上都是直接操作列表内部,不会创建新的一份数据】
user_list = ["车子","妹子"]
user_list.append("嫂子")
print(user_list) # ["车子","妹子","嫂子"]
(1)append()
append():追加,在原列表中尾部追加值。
data_list = []
v1 = input("请输入姓名")
data_list.append(v1)
v2 = input("请输入姓名")
data_list.append(v2)
print(data_list) # ["王力宏","张韶涵"]
#案例
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)
print(message)
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)
(2)extend()
extend():批量追加,将一个列表中的元素逐一添加另外一个列表。
tools = ["搬砖","菜刀","榔头"]
tools.extend( [11,22,33] ) # weapon中的值逐一追加到tools中
print(tools) # ["搬砖","菜刀","榔头",11,22,33]
weapon = ["AK47","M6"]
tools.extend(weapon)
print(tools) # ["搬砖","菜刀","榔头","AK47","M6"]
#拓展 extend()=for循环+append()
tools = ["搬砖","菜刀","榔头"]
weapon = ["AK47","M6"]
for item in weapon:
tools.append(item)
print(tools) # ["搬砖","菜刀","榔头","AK47","M6"]
(3)insert()
insert():在原列表的指定索引位置插入值
user_list = ["苍老师","有坂深雪","大桥未久"]
user_list.insert(0,"马蓉")
user_list.insert(2,"李小璐")
print(user_list) #['马蓉', '苍老师', '李小璐', '有坂深雪', '大桥未久']
(4)remove()
remove():在原列表中根据值删除(从左到右找到的第一个删除;如果没有值的话会报错,使用时要小心)
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
if "Alex" in user_list:
user_list.remove("Alex")
print(user_list)
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
while True:
if "Alex" in user_list:
user_list.remove("Alex")
else:
break
print(user_list)
(5)pop()
pop():根据索引位置删除值
user_list = ["王宝强","陈羽凡","lily","贾乃亮","lily"]
# 0 1 2 3 4
user_list.pop(1)
print(user_list) # ["王宝强","lily","贾乃亮","lily"]
user_list.pop() #删除列表最后一个索引的值
print(user_list) # ["王宝强","lily","贾乃亮"]
item = user_list.pop(1)
print(item) # "lily"
print(user_list) # ["王宝强","贾乃亮"]
(6)clear()
clear(): 清空原列表
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
user_list.clear()
print(user_list) # []
(7)index()
index():根据值获取索引(从左到右找到第一个删除)【慎用,找不到报错】
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
# 0 1 2 3 4
if "Alex" in user_list:
index = user_list.index("Alex")
print(index) # 2
else:
print("不存在")
(8)sort()
sort():排序时应尽量保持列表中的各元素的数据类型统一,否侧当内部元素无法比较时,程序会报错。
# 数字排序
num_list = [11, 22, 4, 5, 11, 99, 88]
print(num_list)
num_list.sort() # 让num_list从小到大排序
num_list.sort(reverse=True) # # 让num_list从大到小排序
print(num_list)
# 字符串排序
user_list = ["王宝强", "Ab陈羽凡", "Alex", "贾乃亮", "贾乃", "1"]
# [29579, 23453, 24378]
# [65, 98, 38472, 32701, 20961]
# [65, 108, 101, 120]
# [49]
print(user_list)
"""
sort的排序原理
[ "x x x" ," x x x x x " ]
"""
user_list.sort()
print(user_list)
(9)reverse()
reverse():反转原列表
user_list = ["王宝强","陈羽凡","lily","贾乃亮","lily"]
user_list.reverse()
print(user_list) #['lily', '贾乃亮', 'lily', '陈羽凡', '王宝强']
3. 公共功能
(1)相加
多个列表相加获取生成一个新的列表,
data = ["赵四","刘能"] + ["宋晓峰","范德彪"]+["1111","222"]
print(data) # ['赵四', '刘能', '宋晓峰', '范德彪', '1111', '222']
v1 = ["赵四","刘能"]
v2 = ["宋晓峰","范德彪"]
v3 = v1 + v2
print(v3) # ["赵四","刘能","宋晓峰","范德彪"]
(2)相乘
列表*整型N:将列表中的元素再创建N份并生成一个新的列表。
data = ["赵四","刘能"] * 2
print(data) # ["赵四","刘能","赵四","刘能"]
(3)运算符in包含
由于列表内部是由多个元素组成,可以通过in来判断元素是否在列表中。注意:用in检查列表元素是否存在时,是采用逐一比较的方式,效率会比较低。
# 案例
user_list = ["狗子","二蛋","三毛","图图"]
if "alex" in user_list:
print("在,把他删除")
user_list.remove("三毛")
else:
print("不在")
(4)获取长度
user_list = ["范德彪","刘华强",'尼古拉斯赵四']
print( len(user_list) ) #3
(5)索引
注意:超出索引范围会报错。 提示:由于字符串是不可变类型,所以他只有索引读的功能,而列表可以进行 读、改、删
# 读
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)
(6)切片
# 读
user_list = ["范德彪","刘华强",'尼古拉斯赵四']
print( user_list[0:2] ) # ["范德彪","刘华强"]
print( user_list[1:] ) #['刘华强', '尼古拉斯赵四']
print( user_list[:-1] ) #['范德彪', '刘华强']
# 改
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
user_list[0:2] = [11, 22, 33, 44]
print(user_list) # 输出 [11, 22, 33, 44, '尼古拉斯赵四']
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
user_list[2:] = [11, 22, 33, 44]
print(user_list) # 输出 ['范德彪', '刘华强', 11, 22, 33, 44]
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
user_list[3:] = [11, 22, 33, 44]
print(user_list) # 输出 ['范德彪', '刘华强', '尼古拉斯赵四', 11, 22, 33, 44]
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
user_list[10000:] = [11, 22, 33, 44]
print(user_list) # 输出 ['范德彪', '刘华强', '尼古拉斯赵四', 11, 22, 33, 44]
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
user_list[-10000:1] = [11, 22, 33, 44]
print(user_list) # 输出 [11, 22, 33, 44, '刘华强', '尼古拉斯赵四']
# 删
user_list = ["范德彪", "刘华强", '尼古拉斯赵四']
del user_list[1:]
print(user_list) # 输出 ['范德彪']
(7)步长
user_list = ["范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能"]
# 0 1 2 3 4
print( user_list[1:4:2] )
new_data = user_list[::-1]
print(new_data)
data_list = ["范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能"]
data_list.reverse()
print(data_list)
(8)for循环
user_list = ["范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能"]
for item in user_list:
print(item)
# 正确方式,倒着删除。
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)
4. 数据类型之间的转换
-
int、bool无法转换成列表
-
str
name = "武沛齐"
data = list(name) # ["武","沛","齐"]
print(data)
-
元组
v1 = (11,22,33,44) # 元组
vv1 = list(v1) # 列表 [11,22,33,44]
-
集合
v2 = {"alex","eric","dsb"} # 集合
vv2 = list(v2) # 列表 ["alex","eric","dsb"]
5. 列表嵌套
列表属于容器,内部可以存放各种数据,所以他也支持列表的嵌套,如:
data = [ "谢广坤",["海燕","赵本山"],True,[11,22,[999,123],33,44],"宋小宝" ]
对于嵌套的值,可以根据之前学习的索引知识点来进行学习,例如:
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) # [ "谢广坤",["海燕","赵本山"],True,[11,22,33,44],"宋小宝",666]
data[1].append("谢大脚")
print(data) # [ "谢广坤",["海燕","赵本山","谢大脚"],True,[11,22,33,44],"宋小宝",666 ]
del data[-2]
print(data) # [ "谢广坤",["海燕","赵本山","谢大脚"],True,[11,22,33,44],666 ]
data[-2][1] = "alex"
print(data) # [ "谢广坤",["海燕","赵本山","谢大脚"],True,[11,"alex",33,44],666 ]
data[1][0:2] = [999,666]
print(data) # [ "谢广坤",[999,666,"谢大脚"],True,[11,"alex",33,44],666 ]
二.元组
列表(list),是一个有序且可变的容器,在里面可以存放多个不同类型的元素。
元组(tuple),是一个有序且不可变的容器,在里面可以存放多个不同类型的元素。
1.定义
注意:建议在元组的最后多加一个逗号,用于标识他是一个元组。
v1 = (11,22,33)
v2 = ("李杰","Alex")
v3 = (True,123,"Alex",[11,22,33,44])
d1 = (1) # 1
d2 = (1,) # (1,)
2.独有功能
无
3.公共功能
(1)相加
两个列表相加获取生成一个新的列表。
data = ("赵四","刘能") + ("宋晓峰","范德彪")
print(data) # ("赵四","刘能","宋晓峰","范德彪")
v1 = ("赵四","刘能")
v2 = ("宋晓峰","范德彪")
v3 = v1 + v2
print(v3) # ("赵四","刘能","宋晓峰","范德彪")
(2)相乘
列表*整型 将列表中的元素再创建N份并生成一个新的列表。
data = ("赵四","刘能") * 2
print(data) # ("赵四","刘能","赵四","刘能")
(3)获取长度
user_list = ("范德彪","刘华强",'尼古拉斯赵四',)
print( len(user_list) )
(4)索引
user_list = ("范德彪","刘华强",'尼古拉斯赵四',)
print( user_list[0] )
print( user_list[2] )
print( user_list[3] )
(5)切片
user_list = ("范德彪","刘华强",'尼古拉斯赵四',)
print( user_list[0:2] )
print( user_list[1:] )
print( user_list[:-1] )
(6)步长
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 = ("范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能")
data = user_list[::-1]
# 列表
user_list = ["范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能"]
data = user_list[::-1]
user_list.reverse()
print(user_list)
(7)for循环
user_list = ("范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能")
for item in user_list:
print(item)
user_list = ("范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能")
for item in user_list:
if item == '刘华强':
continue
print(name)
#目前:只有 str、list、tuple 可以被for循环。 "xxx" [11,22,33] (111,22,33)
# len + range + for + 索引
user_list = ("范德彪","刘华强",'尼古拉斯赵四',"宋小宝","刘能")
for index in range(len(user_list)):
item = user_list[index]
print(item)
4. 数据类型之间的转换
其他类型转换为元组,使用tuple(其他类型)
,目前只有字符串和列表可以转换为元组。
name = "武沛齐"
data = tuple(name)
print(data) # 输出 ("武","沛","齐")
name = ["武沛齐",18,"pythonav"]
data = tuple(name)
print(data) # 输出 ("武沛齐",18,"pythonav")
5.元组嵌套
由于元组和列表都可以充当容器
,他们内部可以放很多元素,并且也支持元素内的各种嵌套。 元组有序且不可变。
data = ("123",666,[11,22,33], ("alex","李杰",[999,666,(5,6,7)]) )
# 1.将 “123” 替换成 9 报错
# 2.将 [11,22,33] 换成 "武沛齐" 报错
# 3.将 11 换成 99
data[2][0] = 99
print(data) # ("123",666,[99,22,33], ("alex","李杰",[999,666,(5,6,7)]) )
# 4.在列表 [11,22,33] 追加一个44
data[2].append(44)
print(data) # ("123",666,[11,22,33,44], ("alex","李杰",[999,666,(5,6,7)]) )