大二暑假第八周总结
上学期选修课程python程序设计,心想是选修就没有好好学,纯纯摆烂了,但是后来发现python真的是太强大了,有点后悔当时没有好好学了,但是该说不说当时上课的时候也确实听了不好啊,所以有一定的基础,所以这次在B站找了一个30小时左右的视频进行观看,然后倍速观看,也跟着视频做案例,然后这一周把python基础看完了,但是我感觉爬虫和数据处理特别神奇,特别想学习一下相关技术,所以找时间看一下。
这次也学习了python的数据容器,这次比之前跟清楚的知道了这些是干什么的,怎么用,接下来写一下这几天学到的知识
""" 演示数据容器之:list列表的常用操作 """ mylist = ["itcast", "itheima", "python"] # 1.1 查找某元素在列表内的下标索引 index = mylist.index("itheima") print(f"itheima在列表中的下标索引值是:{index}") # 1.2如果被查找的元素不存在,会报错 # index = mylist.index("hello") # print(f"hello在列表中的下标索引值是:{index}") # 2. 修改特定下标索引的值 mylist[0] = "传智教育" print(f"列表被修改元素值后,结果是:{mylist}") # 3. 在指定下标位置插入新元素 mylist.insert(1, "best") print(f"列表插入元素后,结果是:{mylist}") # 4. 在列表的尾部追加```单个```新元素 mylist.append("黑马程序员") print(f"列表在追加了元素后,结果是:{mylist}") # 5. 在列表的尾部追加```一批```新元素 mylist2 = [1, 2, 3] mylist.extend(mylist2) print(f"列表在追加了一个新的列表后,结果是:{mylist}") # 6. 删除指定下标索引的元素(2种方式) mylist = ["itcast", "itheima", "python"] # 6.1 方式1:del 列表[下标] del mylist[2] print(f"列表删除元素后结果是:{mylist}") # 6.2 方式2:列表.pop(下标) mylist = ["itcast", "itheima", "python"] element = mylist.pop(2) print(f"通过pop方法取出元素后列表内容:{mylist}, 取出的元素是:{element}") # 7. 删除某元素在列表中的第一个匹配项 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] mylist.remove("itheima") print(f"通过remove方法移除元素后,列表的结果是:{mylist}") # 8. 清空列表 mylist.clear() print(f"列表被清空了,结果是:{mylist}") # 9. 统计列表内某元素的数量 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] count = mylist.count("itheima") print(f"列表中itheima的数量是:{count}") # 10. 统计列表中全部的元素数量 mylist = ["itcast", "itheima", "itcast", "itheima", "python"] count = len(mylist) print(f"列表的元素数量总共有:{count}个")
""" 演示tuple元组的定义和操作 """ # 定义元组 t1 = (1, "Hello", True) t2 = () t3 = tuple() print(f"t1的类型是:{type(t1)}, 内容是:{t1}") print(f"t2的类型是:{type(t2)}, 内容是:{t2}") print(f"t3的类型是:{type(t3)}, 内容是:{t3}") # 定义单个元素的元素 t4 = ("hello", ) print(f"t4的类型是:{type(t4)}, t4的内容是:{t4}") # 元组的嵌套 t5 = ( (1, 2, 3), (4, 5, 6) ) print(f"t5的类型是:{type(t5)}, 内容是:{t5}") # 下标索引去取出内容 num = t5[1][2] print(f"从嵌套元组中取出的数据是:{num}") # 元组的操作:index查找方法 t6 = ("传智教育", "黑马程序员", "Python") index = t6.index("黑马程序员") print(f"在元组t6中查找黑马程序员,的下标是:{index}") # 元组的操作:count统计方法 t7 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python") num = t7.count("黑马程序员") print(f"在元组t7中统计黑马程序员的数量有:{num}个") # 元组的操作:len函数统计元组元素数量 t8 = ("传智教育", "黑马程序员", "黑马程序员", "黑马程序员", "Python") num = len(t8) print(f"t8元组中的元素有:{num}个") # 元组的遍历:while index = 0 while index < len(t8): print(f"元组的元素有:{t8[index]}") # 至关重要 index += 1 # 元组的遍历:for for element in t8: print(f"2元组的元素有:{element}") # 修改元组内容 # t8[0] = "itcast" # 定义一个元组 t9 = (1, 2, ["itheima", "itcast"]) print(f"t9的内容是:{t9}") t9[2][0] = "黑马程序员" t9[2][1] = "传智教育" print(f"t9的内容是:{t9}")
""" 演示以数据容器的角色,学习字符串的相关操作 """ my_str = "itheima and itcast" # 通过下标索引取值 value = my_str[2] value2 = my_str[-16] print(f"从字符串{my_str}取下标为2的元素,。值是:{value},取下标为-16的元素。值是:{value2}") # my_str[2] = "H" # index方法 value = my_str.index("and") print(f"在字符串{my_str}中查找and,其起始下标是:{value}") # replace方法 new_my_str = my_str.replace("it", "程序") print(f"将字符串{my_str},进行替换后得到:{new_my_str}") # split方法 my_str = "hello python itheima itcast" my_str_list = my_str.split(" ") print(f"将字符串{my_str}进行split切分后得到:{my_str_list}, 类型是:{type(my_str_list)}") # strip方法 my_str = " itheima and itcast " new_my_str = my_str.strip() # 不传入参数,去除首尾空格 print(f"字符串{my_str}被strip后,结果:{new_my_str}") my_str = "12itheima and itcast21" new_my_str = my_str.strip("12") print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}") # 统计字符串中某字符串的出现次数, count my_str = "itheima and itcast" count = my_str.count("it") print(f"字符串{my_str}中it出现的次数是:{count}") # 统计字符串的长度, len() num = len(my_str) print(f"字符串{my_str}的长度是:{num}")
""" 演示对序列进行切片操作 """ # 对list进行切片,从1开始,4结束,步长1 my_list = [0, 1, 2, 3, 4, 5, 6] result1 = my_list[1:4] # 步长默认是1,所以可以省略不写 print(f"结果1:{result1}") # 对tuple进行切片,从头开始,到最后结束,步长1 my_tuple = (0, 1, 2, 3, 4, 5, 6) result2 = my_tuple[:] # 起始和结束不写表示从头到尾,步长为1可以省略 print(f"结果2:{result2}") # 对str进行切片,从头开始,到最后结束,步长2 my_str = "01234567" result3 = my_str[::2] print(f"结果3:{result3}") # 对str进行切片,从头开始,到最后结束,步长-1 my_str = "01234567" result4 = my_str[::-1] # 等同于将序列反转了 print(f"结果4:{result4}") # 对列表进行切片,从3开始,到1结束,步长-1 my_list = [0, 1, 2, 3, 4, 5, 6] result5 = my_list[3:1:-1] print(f"结果5:{result5}") # 对元组进行切片,从头开始,到尾结束,步长-2 my_tuple = (0, 1, 2, 3, 4, 5, 6) result6 = my_tuple[::-2] print(f"结果6:{result6}")
""" 演示数据容器集合的使用 """ # 定义集合 my_set = {"传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima", "传智教育", "黑马程序员", "itheima"} my_set_empty = set() # 定义空集合 print(f"my_set的内容是:{my_set}, 类型是:{type(my_set)}") print(f"my_set_empty的内容是:{my_set_empty}, 类型是:{type(my_set_empty)}") # 添加新元素 my_set.add("Python") my_set.add("传智教育") # print(f"my_set添加元素后结果是:{my_set}") # 移除元素 my_set.remove("黑马程序员") print(f"my_set移除黑马程序员后,结果是:{my_set}") # 随机取出一个元素 my_set = {"传智教育", "黑马程序员", "itheima"} element = my_set.pop() print(f"集合被取出元素是:{element}, 取出元素后:{my_set}") # 清空集合, clear my_set.clear() print(f"集合被清空啦,结果是:{my_set}") # 取2个集合的差集 set1 = {1, 2, 3} set2 = {1, 5, 6} set3 = set1.difference(set2) print(f"取出差集后的结果是:{set3}") print(f"取差集后,原有set1的内容:{set1}") print(f"取差集后,原有set2的内容:{set2}") # 消除2个集合的差集 set1 = {1, 2, 3} set2 = {1, 5, 6} set1.difference_update(set2) print(f"消除差集后,集合1结果:{set1}") print(f"消除差集后,集合2结果:{set2}") # 2个集合合并为1个 set1 = {1, 2, 3} set2 = {1, 5, 6} set3 = set1.union(set2) print(f"2集合合并结果:{set3}") print(f"合并后集合1:{set1}") print(f"合并后集合2:{set2}") # 统计集合元素数量len() set1 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5} num = len(set1) print(f"集合内的元素数量有:{num}个") # 集合的遍历 # 集合不支持下标索引,不能用while循环 # 可以用for循环 set1 = {1, 2, 3, 4, 5} for element in set1: print(f"集合的元素有:{element}")
""" 演示数据容器字典的定义 """ # 定义字典 my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77} # 定义空字典 my_dict2 = {} my_dict3 = dict() print(f"字典1的内容是:{my_dict1}, 类型:{type(my_dict1)}") print(f"字典2的内容是:{my_dict2}, 类型:{type(my_dict2)}") print(f"字典3的内容是:{my_dict3}, 类型:{type(my_dict3)}") # 定义重复Key的字典 my_dict1 = {"王力鸿": 99, "王力鸿": 88, "林俊节": 77} print(f"重复key的字典的内容是:{my_dict1}") # 从字典中基于Key获取Value my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77} score = my_dict1["王力鸿"] print(f"王力鸿的考试分数是:{score}") score = my_dict1["周杰轮"] print(f"周杰轮的考试分数是:{score}") # 定义嵌套字典 stu_score_dict = { "王力鸿": { "语文": 77, "数学": 66, "英语": 33 }, "周杰轮": { "语文": 88, "数学": 86, "英语": 55 }, "林俊节": { "语文": 99, "数学": 96, "英语": 66 } } print(f"学生的考试信息是:{stu_score_dict}") # 从嵌套字典中获取数据 # 看一下周杰轮的语文信息 score = stu_score_dict["周杰轮"]["语文"] print(f"周杰轮的语文分数是:{score}") score = stu_score_dict["林俊节"]["英语"] print(f"林俊节的英语分数是:{score}")