掌握Python数据结构知识汇总
2026/7/22 4:41:07 网站建设 项目流程
1.str 字符串
三引号的使用:
文档注释: 一般位于文件开头,使用三引号,来对整个py文件进行注释说明
函数注释:三引号
类注释:三引号

三引号: 格式化字符串
s4 = ''' hello world ''' print(s4)

\' \" \''' \""" 使用引号失去声明字符串的作用 \n 换行 \t制表符缩进 \\ 第一个是转义字符 使第二个失去转义作用称为普通字符
# msg = '123"qwe+-*/!@#¥%……&*()、' 如果字符串中有双引号,则外侧用单引号 # print(msg) # msg2 = "123'qwe+-*/!@#¥%……&*()、" 如果字符串中有单引号,则外侧用双引号 # print(msg)

msg4 = "hello\nworld\thi world" \n 为换行 \t 是加入空格 print(msg4)

msg5 = "C:\\Program Files\\Python313\\new" 如果字符串中有\ 则在其前面再加一个 print(msg5)

name = "qiku" age = 1 addr = "东三街" print("名字叫", name, "年纪是", age, "地址是", addr, sep="") # 使用f print(f"名字叫{name}年纪是{age}地址是{addr}") # 使用%连接 print("名字叫%s年纪是%s地址是%s" % (name, age, addr)) # 使用format函数 print("名字叫{}年纪是{}地址是{}".format(name, age, addr))

# 转小写
print("heLlo WorlD".lower())
# 转大写
print("heLlo WorlD".upper())
# 单词首字母大写
print("heLlo worlD".title())
# 首字母大写
print("heLlo worlD".capitalize())
# 大小写转换
print("heLlo worlD".swapcase())

# 对齐
# 居中
print("hello world".center(20))
print("hello world".center(20, "*"))
# 居左
print("hello world".ljust(20, "*"))
# 居右
print("hello world".rjust(20, "*"))
# 居右 左侧填0
print("hello world".zfill(20))

# 开始结尾
print(" hello world ".startswith(" "))
print(" hello world ".endswith(" "))

# 剔除空白
print(" hello world ".strip())
print("+++hello world+++".strip("+"))
print("+++hello world+++".lstrip("+"))
print("+++hello world+++".rstrip("+"))

# 拼接
print("++".join("hello world"))
print(" ".join(['hello', 'world']))

# 切割 默认空格
print("hello world".split())
print("hello world".split('l'))
# 替换 count 指明替换次数
print("hello world hello china".replace("hello", "hi", 1))

# bytes 字符串前有字符 b''
print("hello world 123 中国".encode("gbk"))
# 解码
print(b'hello world 123 \xd6\xd0\xb9\xfa'.decode("gbk"))


# is*
print("az".isalpha())
print("19".isdigit())
print("19az".isalnum())

print("Az".islower())
print("Az".isupper())
print("Az Bz".istitle())

2.list 列表

定义

在Python等编程语言里,列表是一种专种专门用来存储多个数据的容器。

总结一句话:Python列表就是时一个带编号的、可变的、什么都能装的大口袋,是写代码存数据最顺手的基础工具。

索引

列表索引其实就是列表中每个元素的“门牌号”或“座位号”,有了它,你才能准确地找到、拿到或者修改列表里的某个具体值。

在Python里,索引永远从0开始数,而不是从1开始

print(["x", "b", "a", "a"].index("a")) #查找列表中 a 所在的位置,如果查找到元素没在列表则报错

print(["a", "b", "a", "a"].count("a")) #查找元素 a 所在列表中所出现的次数
datas = [1, 2, 3] datas.append(4) #在列表中插入元素 4

datas.insert(1, 2.5) #在列表中一的位置中插入元素 2.5

datas.extend([5, 6, 7]) #在列表后面插入元素 5 6 7

datas.pop() print(datas) 剔除最后 一位元素

切片

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 从索引2开始,到索引6之前结束(不包含6)

numbers[2:6]

结果

[2, 3, 4, 5] 从0开始,每隔2个取一个(取偶数)

结果

[0, 2, 4, 6, 8]

numbers[::2]

遍历

datas = [90, 85, 78, 92] for i in range(len(datas)): print(datas[i]) 打印出列表中的每一个元素

3.tuple 元组

元组,简单来说,就是一个有序的、不可变的元素集合
创建单元素元组时,必须加逗号
利用类型转换将可迭代类型转为元组 t3 = tuple("hello") print(t3, type(t3))

元组汇总可以放入任意类型 t4 = (10, 3.14, "hello", True, None, [1,2,3], (1,2,3)) print(t4, type(t4))

元组是不可变数据类型 一旦定义就不能修改
对变量从新赋值 字符串和元组对应的内存并没有修改 s = "hello world" s = "hello xorld" print(s)


4.dict 字典

字典是编程中另一个核心的数据结构,它和元组(以及列表)解决的是完全不同的问题。

字典,简单来说,就是一个无序的、可变的“键-值”(Key-Value)映射集合

它不靠位置(如第0个、第1个)来查找数据,而是靠一个独特的“钥匙”(键)来获取对应的“内容”(值)

d0 = { "id": 100, "nama": "马云", "name": "马化腾" } #删除指定键并且返回的应的值 v = d0.pop("nama") print(v, d0)

d0 = { "id": 100, "nama": "马云", "name": "马化腾" } d0["id"] = 123 d0["age"] = 40 print(d0, "现有字典") # 字典的存储不分先后顺序 # 字典键插入有现有顺序

d0 = { "id": 100, "nama": "马云", "name": "马化腾" } d0["id"] = 123 d0["age"] = 40 #popitem删除 最晚插入的键值对 r = d0.popitem() print(r, d0)

# 构建一个所有键对应的值都一样的字典 d0 = dict.fromkeys(["key1", "key2", "key3"], 888) print(d0)

d0 = { "id": 100, "name": "马云", "age": 20, } if "age" in d0: print(d0["age"]) # 直接获取一个键对应的值 如果存在则返回 如果不存在则返回None 除非提供了default


5.set 集合
# 创建空集合的唯一方式 s2 = set() print(type(s2))
# 使用iterable可迭代类型 转换为集合 s3 = set("hello") print(s3, type(s3))

# 可变类型 list dict set 都不可以作为集合的元素 # 集合中可以放入不可变数据类型 int float str bool NoneType tuple s4 = {10, 3.14, "hello", True, None, (1, 2, 3)} print(s4, type(s4))

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询