详细介绍Python3数据结构知识点
本篇文章给大家带来了关于python的相关知识,其中主要介绍了数据结构的相关问题,包括了数字、字符串、列表、元组、字典等等的相关内容,希望对大家有帮助。
推荐学习:python视频教程
数字 Number
数字类型转换
数字运算
# + - * / %(取余) **(幂运算)# 整数除法中,除法 / 总是返回一个浮点数,# 如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 //print(8 / 5) # 1.6print(8 // 5) # 1# 注意:// 得到的并不一定是整数类型的数,它与分母分子的数据类型有关系print(8 // 5.0) # 1.0# 使用 ** 操作来进行幂运算print(5 ** 2) # 5的平方 25
字符串 str
字符串的查询
s = 'hello, hello'print(s.index('lo')) # 3print(s.find('lo')) # 3print(s.find('k')) # -1print(s.rindex('lo')) # 10print(s.rfind('lo')) # 10
字符串大小写转换
s = 'hello, Python'print(s.upper()) # HELLO, PYTHONprint(s.lower()) # hello, pythonprint(s.swapcase()) # HELLO, pYTHONprint(s.capitalize()) # Hello, pythonprint(s.title()) # Hello, Python
字符串对齐
s = 'hello,Python''''居中对齐'''print(s.center(20, '*')) # ****hello,Python****'''左对齐 '''print(s.ljust(20, '*')) # hello,Python********print(s.ljust(5, '*')) # hello,Python'''右对齐'''print(s.rjust(20, '*')) # ********hello,Python'''右对齐,使用0进行填充'''print(s.zfill(20)) # 00000000hello,Pythonprint('-1005'.zfill(8)) # -0001005
字符串拆分、切片
拆分
s = 'hello word Python'print(s.split()) # ['hello', 'word', 'Python']s1 = 'hello|word|Python'print(s1.split(sep='|')) # ['hello', 'word', 'Python']print(s1.split('|', 1)) # ['hello', 'word|Python'] # 左侧开始print(s1.rsplit('|', 1)) # ['hello|word', 'Python'] # 右侧开始
切片
s = 'hello,world'print(s[:5]) # hello 从索引0开始,到4结束print(s[6:]) # world 从索引6开始,到最后一个元素print(s[1:5:1]) # ello 从索引1开始,到4结束,步长为1print(s[::2]) # hlowrd 从开始到结束,步长为2print(s[::-1]) # dlrow,olleh 步长为负数,从最后一个元素(索引-1)开始,到第一个元素结束print(s[-6::1]) # ,world 从索引-6开始,到最后一个结束
字符串判断相关
字符串其他操作
字符串替换
s = 'hello,Python,Python,Python'print(s.replace('Python', 'Java')) # 默认全部替换 hello,Java,Java,Javaprint(s.replace('Python', 'Java', 2)) # 设置替换个数 hello,Java,Java,Python
字符串连接
lst = ['hello', 'java', 'Python']print(','.join(lst)) # hello,java,Pythonprint('|'.join(lst)) # hello|java|Python
格式化字符串输出
name = '张三'age = 20print('我叫%s, 今年%d岁' % (name, age))print('我叫{0}, 今年{1}岁,小名也叫{0}'.format(name, age))print(f'我叫{name}, 今年{age}岁')# 我叫张三, 今年20岁# 我叫张三, 今年20岁,小名也叫张三# 我叫张三, 今年20岁
设置数字的宽度和精度
# 设置数字的宽度和精度'''%占位'''print('%10d' % 99) # 10表示宽度print('%.3f' % 3.1415926) # .3f表示小数点后3位print('%10.3f' % 3.1415926) # 同时设置宽度和精度'''{}占位 需要使用:开始'''print('{:.3}'.format(3.1415926)) # .3表示3位有效数字print('{:.3f}'.format(3.1415926)) # .3f表示小数点后3位print('{:10.3f}'.format(3.1415926)) # .3f表示小数点后3位# 99#3.142# 3.142#3.14#3.142# 3.142
字符串编码
s = '但愿人长久'# 编码 将字符串转换成byte(二进制)数据print(s.encode(encoding='gbk')) #gbk,中文占用2个字节print(s.encode(encoding='utf-8')) #utf-8,中文占用3个字节# 解码 将byte(二进制)转换成字符串数据# 编码与解码中,encoding方式需要一致byte = s.encode(encoding='gbk')print(byte.decode(encoding='gbk'))# b'��Ը�˳���'# b'但愿人长久'# 但愿人长久
列表 list
列表的特点
列表的创建
列表元素的查询
- 判断指定元素在列表中是否存在
in / not in
- 列表元素的遍历
for item in list:print(item)
- 查询元素索引
list.index(item)
- 获取元素
list = [1, 4, 9, 16, 25, 36, 49, 64, 81]print(list[3]) # 16print(list[3:6]) # [16, 25, 36]
列表元素的增加
列表元素的删除
列表元素的排序
list.sort()
sorted(list)
知识点总结
元组 tuple
元组的特点
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号
元组的创建
t = ('Python', 'hello', 90)
tuple(('Python', 'hello', 90))
t = (10,)
知识点总结
字典 dict
字典的特点
字典的创建
items = ['fruits', 'Books', 'Others']prices = [12, 36, 44]d = {item.upper(): price for item, price in zip(items, prices)}print(d) # {'FRUITS': 12, 'BOOKS': 36, 'OTHERS': 44}
字典元素的获取
字典元素的新增
user = {"id": 1, "name": "zhangsan"}user["age"] = 25print(user) # {'id': 1, 'name': 'zhangsan', 'age': 25}
字典元素的修改
user = {"id": 1, "name": "zhangsan", "age": 25}user["age"] = 18print(user) # {'id': 1, 'name': 'zhangsan', 'age': 18}
字典元素的删除
user = {"id": 1, "name": "zhangsan"}del user["id"]print(user) # {'name': 'zhangsan'}del user
user = {"id": 1, "name": "zhangsan"}user.clear()print(user) # {}
获取字典视图
字典元素的遍历
scores = {'张三': 100, '李四': 95, '王五': 88}for name in scores: print(name, scores[name])
scores = {'张三': 100, '李四': 95, '王五': 88}for name, score in scores.items(): print(name, score)
知识点总结
集合 set
集合的特点
集合的创建
s = {'Python', 'hello', 90}
print(set("Python"))print(set(range(1,6)))print(set([3, 4, 7]))print(set((3, 2, 0)))print(set({"a", "b", "c"}))# 定义空集合:set()print(set())
print({i * i for i in range(1, 10)})# {64, 1, 4, 36, 9, 16, 49, 81, 25}
集合的操作
- 集合元素的判断操作
- 集合元素的新增操作
- 集合元素的删除操作
集合间的关系
两个集合是否相等:可以使用运算符 == 或 != 进行判断,只要元素相同就相等
一个集合是否是另一个集合的子集:issubset()
s1 = {10, 20, 30, 40, 50, 60}s2 = {10, 30, 40}s3 = {10, 70}print(s2.issubset(s1)) # Trueprint(s3.issubset(s1)) # False
- 一个集合是否是另一个集合的超集:issuperset()
print(s1.issuperset(s2)) # Trueprint(s1.issuperset(s3)) # False
- 两个集合是否无交集:isdisjoint()
s1 = {10, 20, 30, 40, 50, 60}s2 = {10, 30, 40}s3 = {20, 70}print(s1.isdisjoint(s2)) # False 有交集print(s3.isdisjoint(s2)) # True 无交集
集合的数学操作
- 交集: intersection() 与 &等价,两个集合的交集
s1 = {10, 20, 30, 40}s2 = {20, 30, 40, 50, 60}print(s1.intersection(s2)) # {40, 20, 30}print(s1 & s2) # {40, 20, 30}
- 并集: union() 与 | 等价,两个集合的并集
print(s1.union(s2)) # {40, 10, 50, 20, 60, 30}print(s1 | s2) # {40, 10, 50, 20, 60, 30}
- 差集: difference() 与 - 等价
print(s2.difference(s1)) # {50, 60}print(s2 - s1) # {50, 60}
- 对称差集:symmetric_difference() 与 ^ 等价
print(s2.symmetric_difference(s1)) # {10, 50, 60}print(s2 ^ s1) # {10, 50, 60}
知识点总结
列表、元组、字典、集合总结
推荐学习:python教程