Files
python/python_6_数据容器2.py
2025-08-05 09:19:34 +08:00

159 lines
4.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# # 字符串
# my_str = "tom and jerry"
# value = my_str[2]
# value1 = my_str[-12]
# print(f"字符串中取出下标为2的字符为{value},取出下标为-12的字符为{value1}")
# # 字符串常用方法index
# value2 = my_str.index("tom")
# print(f"字符串查找tom,其起始下标为{value2}")
# # 字符串常用方法,replace
# new_my_str = my_str.replace("jerry", "xi_mi")
# print(new_my_str)
# print(my_str)
# # 字符串常用方法 split
# my_str_list = my_str.split(" ")
# print(f"{my_str_list}")
# # 字符串规整操作 strip
# my_str1 = " hello world "
# new_my_str1 = my_str1.strip()
# print(f"{new_my_str1}")
# my_str2 = "12hello 12world21"
# new_my_str2 = my_str2.strip("12")
# print(f"字符串{my_str2}被strip12)后结果为{new_my_str2}")
# # 字符串中某字符串出现的次数,count
# count = my_str.count("a")
# print(f"{count}")
# # 统计字符串长度 len()
# len1 = len(my_str)
# print(f"{len1}")
# 字符串遍历
# index = 0
# my_str = "Good morning"
# while index < len(my_str):
# element = my_str[index]
# index += 1
# print(f"{element}")
# my_str1 = "Good afternoon"
# for element in my_str1:
# print(f"{element}")
# # 序列切片
# # 对list切片,从1开始,4结束,步长1
# my_list = [0, 1, 2, 3, 4, 5, 6]
# result1 = my_list[1:4] # 步长为1,可省略
# print(f"{result1}")
# # 从头开始,到最后结束,步长为2
# result2 = my_list[::2]
# print(f"{result2}")
# # 对str切片,从头开始到最后结束,步长-1
# my_str = "01234567"
# result3 = my_str[::-1]
# print(f"{result3}")
# # 对元组切片,从头开始,到尾结束,步长-2
# my_tuple=(0,1,2,3,4,5,6,7)
# result4=my_tuple[::-2]
# print(f"{result4}")
# # 集合
# my_set = {"tom", "jerry", "python", "element", "tom"}
# my_set_empty = set()
# print(f"me_set的内容是:{my_set},类型是{type(my_set)}")
# print(f"me_set_empty的内容是:{my_set_empty},类型是{type(my_set_empty)}")
# # 添加元素
# my_set.add("heima")
# my_set.add("python")
# print(f"{my_set}")
# # 移除元素
# my_set.remove("jerry")
# print(f"{my_set}")
# # 随机取出一个元素
# element = my_set.pop() # 此操作为随机取出
# print(f"{my_set}")
# # 清空集合
# result= my_set.clear()
# print(f"{my_set}")
# # 取出两个集合的差集
# set1 = {1, 2, 3}
# set2 = {1, 4, 6}
# set3 = set1.difference(set2)
# print(set3)
# print(set1)
# # 消除差集
# set1.difference_update(set2)
# print(f"消除差集后,集合1的结果是{set1}")
# print(f"消除差集后,集合2的结果是{set2}") # 只对集合1消除,集合2不变
# 两集合合并
# set4=set1.union(set2)
# print(f"两集和合并结果为{set4}")
# print(f"合并后集合1为{set1}")
# print(f"合并后集合2为{set2}")#合并后原集合不变
# #集合统计
# num=len(set4)
# print(num)
# 集合的遍历
# 集合不支持下标索引,所以不能用while循环
# set1 = {1, 2,3,4,5,6}
# for element in set1:
# print(f"集合的元素有{element}")
# # 字典
# my_dict = {"王力宏": 99, "汤姆": 88, "林俊杰": 77}
# my_dict2 = dict() # my_dict2={}
# print(f"my_dict的内容是{my_dict},类型 为{type(my_dict)}")
# print(f"my_dict2的内容是{my_dict2},类型 为{type(my_dict2)}")
# # 定义重复字典
# my_dict1 = {"王力宏": 99, "王力宏": 88}
# print(f"重复定义的结果{my_dict1}")
# 从字典基于key获取value
# result = my_dict["王力宏"]
# result1 = my_dict["林俊杰"]
# print(f"王力宏的考试分数为:{result}")
# print(f"王力宏的考试分数为:{result1}")
# # 字典嵌套
# stu_score_dict = {
# "王力宏":{
# "语文":77,
# "数学":88,
# "英语":66,
# },
# "周杰伦":{
# "语文":99,
# "数学":99,
# "英语":99,
# }
# }
# score = stu_score_dict["周杰伦"]["语文"]
# print(f"周杰伦的语文信息为{score}")
# # 字典常用操作
# my_dict = {"周杰伦": 33, "林俊杰": 24, "张学友": 88}
# my_dict["张信哲"] = 32
# print(f"更新后my_dict结果是{my_dict}")
# my_dict["周杰伦"] = 38
# print(f"更新后my_dict结果是{my_dict}")
# # 删除元素
# score = my_dict.pop("周杰伦")
# print(f"字典中被删除了一个元素,结果为{my_dict},周杰伦的考试分数为{score}")
# my_dict.clear()
# print(f"字典被清空了,内容是{my_dict}")
# 获取全部的key
my_dict = {"周杰伦": 33, "林俊杰": 24, "张学友": 88}
keys = my_dict.keys()
print(f"字典的全部key是{keys}")
# 遍历字典
# 方式1通过获取全部的key来完成遍历
for key in my_dict.keys():
print(f"字典的key是:{key}")
print(f"字典的value是:{my_dict[key]}")
# 方式2 直接对字典进行for循环,每次循环都是直接得到key
for key in my_dict:
print(f"字典的key是:{key}")
print(f"字典的value是:{my_dict[key]}")
# 统计字典的数量,len()
num = len(my_dict)
print(f"字典中的key数量为{num}")