简述

6. 组合数据类型 - 图1

掌握组合数据类型(集合、元组、列表、字典)的使用方法,掌握 jieba 库,能够处理一组数据,并处理中文文本。

课件

6.pdf

代码汇总

  1. A = {"python", 123, ("python", 123)}
  2. B = set("pypy123")
  3. C = {"python", 123, "python",123}
  4. print('A:', A)
  5. print('B:', B)
  6. print('C:', C)
  7. '''
  8. A: {('python', 123), 123, 'python'}
  9. B: {'y', 'p', '2', '1', '3'}
  10. C: {123, 'python'}
  11. '''
  1. A = {"p", "y", 123}
  2. B = set("pypy123")
  3. print('A =>', A)
  4. print('B =>', B)
  5. print('A - B =>', A - B)
  6. print('A & B =>', A & B)
  7. print('A ^ B =>', A ^ B)
  8. print('B - A =>', B - A)
  9. print('A | B =>', A | B)
  10. '''
  11. A => {'p', 123, 'y'}
  12. B => {'p', '3', '1', '2', 'y'}
  13. A - B => {123}
  14. A & B => {'p', 'y'}
  15. A ^ B => {'3', '1', 123, '2'}
  16. B - A => {'1', '2', '3'}
  17. A | B => {'p', '3', '1', 123, '2', 'y'}
  18. '''
  1. A = {"p", "y", 123}
  2. print('A =>', A)
  3. for item in A:
  4. print(item, end="")
  5. '''
  6. A => {'p', 'y', 123}
  7. py123
  8. '''
  1. A = {"p", "y", 123}
  2. print('A =>', A)
  3. try:
  4. while True:
  5. print(A.pop(), end="")
  6. except:
  7. pass
  8. '''
  9. A => {'p', 'y', 123}
  10. py123
  11. '''
  1. print("p" in {"p", "y", 123})
  2. print({"p", "y"} >= {"p", "y", 123})
  3. print({"p", "y"} <= {"p", "y", 123})
  4. '''
  5. True
  6. False
  7. True
  8. '''
  1. ls = ["p", "p", "y", "y", 123]
  2. s = set(ls) # 利用了集合无重复元素的特点
  3. print('s =>', s)
  4. lt = list(s) # 还可以将集合转换为列表
  5. print('lt =>', lt)
  6. '''
  7. s => {123, 'p', 'y'}
  8. lt => [123, 'p', 'y']
  9. '''
  1. ls = ["python", 123, ".io"]
  2. print('ls[::-1] =>', ls[::-1])
  3. print('len(ls) =>', len(ls))
  4. s = 'python123.io'
  5. print('s[::-1] =>', s[::-1])
  6. print('max(s) =>', max(s))
  7. '''
  8. ls[::-1] => ['.io', 123, 'python']
  9. len(ls) => 3
  10. s[::-1] => oi.321nohtyp
  11. max(s) => y
  12. '''
  1. creature = "car", "dog", "trigger", "human" # 定义元组类型可不加小括号
  2. print("creature =>", creature)
  3. print("creature[::-1] =>", creature[::-1])
  4. print("creature =>", creature)
  5. color = (0x001100, "blue", creature)
  6. print("color =>", color)
  7. print("color[-1][2] =>", color[-1][2])
  8. '''
  9. creature => ('car', 'dog', 'trigger', 'human')
  10. creature[::-1] => ('human', 'trigger', 'dog', 'car')
  11. creature => ('car', 'dog', 'trigger', 'human')
  12. color => (4352, 'blue', ('car', 'dog', 'trigger', 'human'))
  13. color[-1][2] => trigger
  14. '''
  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. print('ls =>', ls)
  3. lt = ls # 引用赋值
  4. print('lt =>', lt)
  5. lt[0] = 'Cat'
  6. print('ls =>', ls)
  7. print('lt =>', lt)
  8. '''
  9. ls => ['cat', 'dog', 'trigger', 1024]
  10. lt => ['cat', 'dog', 'trigger', 1024]
  11. ls => ['Cat', 'dog', 'trigger', 1024]
  12. lt => ['Cat', 'dog', 'trigger', 1024]
  13. '''
  14. '''
  15. notes:
  16. lt = ls
  17. 这种写法是引用赋值
  18. 意味着 lt、ls 指向的是同一块内存空间
  19. 当我们看到 [] 或者 list() 才意味着新开辟了一块内存
  20. '''
  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. ls[1:2] = [1, 2, 3, 4]
  3. print('ls =>', ls)
  4. del ls[::3]
  5. print('ls =>', ls)
  6. ls = ls * 2
  7. print('ls =>', ls)
  8. '''
  9. ls => ['cat', 1, 2, 3, 4, 'trigger', 1024]
  10. ls => [1, 2, 4, 'trigger']
  11. ls => [1, 2, 4, 'trigger', 1, 2, 4, 'trigger']
  12. '''
  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. ls.append(1234)
  3. print('ls =>', ls)
  4. ls.insert(3, 'human')
  5. print('ls =>', ls)
  6. ls.reverse()
  7. print('ls =>', ls)
  8. '''
  9. ls => ['cat', 'dog', 'trigger', 1024, 1234]
  10. ls => ['cat', 'dog', 'trigger', 'human', 1024, 1234]
  11. ls => [1234, 1024, 'human', 'trigger', 'dog', 'cat']
  12. '''
  1. lt = []
  2. print('1. 定义空列表lt', lt)
  3. lt += [1,2,3,4,5]
  4. print('2. 向lt新增5个元素', lt)
  5. lt[2] = 6
  6. print('3. 修改lt中第2个元素(这里的“第2个”指索引为 2)', lt)
  7. lt.insert(2, 7)
  8. print('4. 向lt中第2个位置增加一个元素', lt)
  9. del lt[1]
  10. print('5. 从lt中第1个位置删除一个元素', lt)
  11. del lt[1:4]
  12. print('6. 删除lt中第1-3位置元素', lt)
  13. rt7 = (0 in lt)
  14. print('7. 判断lt中是否包含数字0', lt, rt7)
  15. lt.append(0)
  16. print('8. 向lt新增数字0', lt)
  17. rt9 = lt.index(0)
  18. print('9. 返回数字0所在lt中的索引', lt, rt9)
  19. rt10 = len(lt)
  20. print('10. lt的长度', rt10)
  21. rt11 = max(lt)
  22. print('11. lt中最大元素', lt, rt11)
  23. lt.clear()
  24. print('12. 清空lt', lt)
  25. '''
  26. 1. 定义空列表lt []
  27. 2. 向lt新增5个元素 [1, 2, 3, 4, 5]
  28. 3. 修改lt中第2个元素(这里的“第2个”指索引为 2) [1, 2, 6, 4, 5]
  29. 4. 向lt中第2个位置增加一个元素 [1, 2, 7, 6, 4, 5]
  30. 5. 从lt中第1个位置删除一个元素 [1, 7, 6, 4, 5]
  31. 6. 删除lt中第1-3位置元素 [1, 5]
  32. 7. 判断lt中是否包含数字0 [1, 5] False
  33. 8. 向lt新增数字0 [1, 5, 0]
  34. 9. 返回数字0所在lt中的索引 [1, 5, 0] 2
  35. 10. lt的长度 3
  36. 11. lt中最大元素 [1, 5, 0] 5
  37. 12. 清空lt []
  38. '''
  1. # 如果不希望数据被程序所改变,转换成元组类型
  2. ls = ['cat', 'dog', 'trigger', 1024]
  3. lt = tuple(ls)
  4. print(lt) # ('cat', 'dog', 'trigger', 1024)
  1. def getNum(): # 获取用户不定长度的输入
  2. nums = []
  3. iNumStr = input("请输入数字(回车退出): ")
  4. while iNumStr != "":
  5. nums.append(eval(iNumStr))
  6. iNumStr = input("请输入数字(回车退出): ")
  7. return nums
  8. def mean(numbers): # 计算平均值
  9. s = 0.0
  10. for num in numbers:
  11. s = s + num
  12. return s / len(numbers)
  13. def dev(numbers, mean): # 计算方差
  14. sdev = 0.0
  15. for num in numbers:
  16. sdev = sdev + (num - mean)**2
  17. return pow(sdev / (len(numbers)-1), 0.5)
  18. def median(numbers): # 计算中位数
  19. sorted(numbers)
  20. size = len(numbers)
  21. if size % 2 == 0:
  22. med = (numbers[size//2-1] + numbers[size//2])/2
  23. else:
  24. med = numbers[size//2]
  25. return med
  26. n = getNum() # 主体函数
  27. m = mean(n)
  28. print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n, m), median(n)))
  29. '''
  30. 请输入数字(回车退出): 10
  31. 请输入数字(回车退出): 20
  32. 请输入数字(回车退出): 30
  33. 请输入数字(回车退出):
  34. 平均值:20.0,方差:1e+01,中位数:20.
  35. '''
  1. d = {"中国": "北京", "美国": "华盛顿", "法国": "巴黎"}
  2. print("d =>", d)
  3. print('d["中国"] =>', d["中国"])
  4. print('type(d) =>', type(d))
  5. '''
  6. d => {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  7. d["中国"] => 北京
  8. type(d) => <class 'dict'>
  9. '''
  1. d = {"中国": "北京", "美国": "华盛顿", "法国": "巴黎"}
  2. print('1.', "中国" in d)
  3. print('2.', d.keys())
  4. print('3.', d.values())
  5. print('4.', d.get("中国", "伊斯兰堡"))
  6. print('5.', d)
  7. print('6.', d.get("巴基斯坦", "伊斯兰堡"))
  8. print('7.', d)
  9. print('8.', d.popitem())
  10. print('9.', d)
  11. '''
  12. 1. True
  13. 2. dict_keys(['中国', '美国', '法国'])
  14. 3. dict_values(['北京', '华盛顿', '巴黎'])
  15. 4. 北京
  16. 5. {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  17. 6. 伊斯兰堡
  18. 7. {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  19. 8. ('法国', '巴黎')
  20. 9. {'中国': '北京', '美国': '华盛顿'}
  21. '''
  1. # 1. 定义空字典d
  2. d = {}
  3. print("1.", d)
  4. # 2. 向d新增2个键值对元素
  5. d["a"] = 1
  6. d["b"] = 2
  7. print("2.", d)
  8. # 3. 修改第2个添加元素
  9. d["b"] = 3
  10. print("3.", d)
  11. # 4. 判断字符"c"是否是d的键
  12. "c" in d
  13. print("4.", "c" in d)
  14. # 5. 计算d的长度
  15. len(d)
  16. print("5.", len(d))
  17. # 6. 清空d
  18. d.clear()
  19. print('6.', d)
  20. """
  21. 1. {}
  22. 2. {'a': 1, 'b': 2}
  23. 3. {'a': 1, 'b': 3}
  24. 4. False
  25. 5. 2
  26. 6. {}
  27. """
  1. def getText():
  2. txt = open("hamlet.txt", "r").read()
  3. txt = txt.lower()
  4. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  5. txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
  6. return txt
  7. hamletTxt = getText()
  8. words = hamletTxt.split()
  9. counts = {}
  10. for word in words:
  11. counts[word] = counts.get(word, 0) + 1
  12. items = list(counts.items())
  13. items.sort(key=lambda x: x[1], reverse=True)
  14. for i in range(10):
  15. word, count = items[i]
  16. print("{0:<10}{1:>5}".format(word, count))
  17. '''
  18. the 1138
  19. and 965
  20. to 754
  21. of 669
  22. you 550
  23. i 542
  24. a 542
  25. my 514
  26. hamlet 462
  27. in 436
  28. '''
  1. import jieba
  2. txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
  3. words = jieba.lcut(txt)
  4. counts = {}
  5. for word in words:
  6. if len(word) == 1:
  7. continue
  8. else:
  9. counts[word] = counts.get(word,0) + 1
  10. items = list(counts.items())
  11. items.sort(key=lambda x:x[1], reverse=True)
  12. for i in range(15):
  13. word, count = items[i]
  14. print ("{0:<10}{1:>5}".format(word, count))
  15. '''
  16. 曹操 953
  17. 孔明 836
  18. 将军 772
  19. 却说 656
  20. 玄德 585
  21. 关公 510
  22. 丞相 491
  23. 二人 469
  24. 不可 440
  25. 荆州 425
  26. 玄德曰 390
  27. 孔明曰 390
  28. 不能 384
  29. 如此 378
  30. 张飞 358
  31. '''
  1. import jieba
  2. excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此"}
  3. txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
  4. words = jieba.lcut(txt)
  5. counts = {}
  6. for word in words:
  7. if len(word) == 1:
  8. continue
  9. elif word == "诸葛亮" or word == "孔明曰":
  10. rword = "孔明"
  11. elif word == "关公" or word == "云长":
  12. rword = "关羽"
  13. elif word == "玄德" or word == "玄德曰":
  14. rword = "刘备"
  15. elif word == "孟德" or word == "丞相":
  16. rword = "曹操"
  17. else:
  18. rword = word
  19. counts[rword] = counts.get(rword, 0) + 1
  20. for word in excludes:
  21. del counts[word]
  22. items = list(counts.items())
  23. items.sort(key=lambda x: x[1], reverse=True)
  24. for i in range(10):
  25. word, count = items[i]
  26. print("{0:<10}{1:>5}".format(word, count))
  27. '''
  28. 曹操 1451
  29. 孔明 1383
  30. 刘备 1252
  31. 关羽 784
  32. 张飞 358
  33. 商议 344
  34. 如何 338
  35. 主公 331
  36. 军士 317
  37. 吕布 300
  38. '''
  1. s = '''双儿 洪七公 赵敏 赵敏 逍遥子 鳌拜 殷天正 金轮法王 乔峰 杨过 洪七公 郭靖
  2. 杨逍 鳌拜 殷天正 段誉 杨逍 慕容复 阿紫 慕容复 郭芙 乔峰 令狐冲 郭芙
  3. 金轮法王 小龙女 杨过 慕容复 梅超风 李莫愁 洪七公 张无忌 梅超风 杨逍
  4. 鳌拜 岳不群 黄药师 黄蓉 段誉 金轮法王 忽必烈 忽必烈 张三丰 乔峰 乔峰
  5. 阿紫 乔峰 金轮法王 袁冠南 张无忌 郭襄 黄蓉 李莫愁 赵敏 赵敏 郭芙 张三丰
  6. 乔峰 赵敏 梅超风 双儿 鳌拜 陈家洛 袁冠南 郭芙 郭芙 杨逍 赵敏 金轮法王
  7. 忽必烈 慕容复 张三丰 杨逍 令狐冲 黄药师 袁冠南 杨逍 完颜洪烈 殷天正
  8. 李莫愁 阿紫 逍遥子 乔峰 逍遥子 完颜洪烈 郭芙 杨逍 张无忌 杨过 慕容复
  9. 逍遥子 虚竹 双儿 乔峰 郭芙 黄蓉 李莫愁 陈家洛 杨过 忽必烈 鳌拜 王语嫣
  10. 洪七公 韦小宝 阿朱 梅超风 段誉 岳灵珊 完颜洪烈 乔峰 段誉 杨过 杨过 慕容复
  11. 黄蓉 杨过 阿紫 杨逍 张三丰 张三丰 赵敏 张三丰 杨逍 黄蓉 金轮法王 郭襄
  12. 张三丰 令狐冲 郭芙 韦小宝 黄药师 阿紫 韦小宝 金轮法王 杨逍 令狐冲 阿紫
  13. 洪七公 袁冠南 双儿 郭靖 鳌拜 谢逊 阿紫 郭襄 梅超风 张无忌 段誉 忽必烈
  14. 完颜洪烈 双儿 逍遥子 谢逊 完颜洪烈 殷天正 金轮法王 张三丰 双儿 郭襄 阿朱
  15. 郭襄 双儿 李莫愁 郭襄 忽必烈 金轮法王 张无忌 鳌拜 忽必烈 郭襄 令狐冲
  16. 谢逊 梅超风 殷天正 段誉 袁冠南 张三丰 王语嫣 阿紫 谢逊 杨过 郭靖 黄蓉
  17. 双儿 灭绝师太 段誉 张无忌 陈家洛 黄蓉 鳌拜 黄药师 逍遥子 忽必烈 赵敏
  18. 逍遥子 完颜洪烈 金轮法王 双儿 鳌拜 洪七公 郭芙 郭襄'''
  19. ls = s.split()
  20. ss = set(ls)
  21. print(len(ss)) # 36
  1. s = input()
  2. try:
  3. d = eval(s)
  4. e = {}
  5. for k in d:
  6. e[d[k]] = k
  7. print(e)
  8. except:
  9. print("输入错误")
  10. '''
  11. {"a": 1, "b": 2}
  12. {1: 'a', 2: 'b'}
  13. '''
  1. import jieba
  2. f = open("沉默的羔羊.txt", encoding='utf-8')
  3. ls = jieba.lcut(f.read())
  4. d = {}
  5. for w in ls:
  6. if len(w) >= 2:
  7. d[w] = d.get(w, 0) + 1
  8. maxc = 0
  9. maxw = ""
  10. for k in d:
  11. if d[k] > maxc:
  12. maxc = d[k]
  13. maxw = k
  14. elif d[k] == maxc and k > maxw:
  15. maxw = k
  16. print(maxw) # 史达琳
  17. f.close()
  1. n = input()
  2. ss = set(n)
  3. s = 0
  4. for i in ss:
  5. s += eval(i)
  6. print(s)
  7. '''
  8. 123123123
  9. 6
  10. '''
  1. s = '''双儿 洪七公 赵敏 赵敏 逍遥子 鳌拜 殷天正 金轮法王 乔峰 杨过 洪七公 郭靖
  2. 杨逍 鳌拜 殷天正 段誉 杨逍 慕容复 阿紫 慕容复 郭芙 乔峰 令狐冲 郭芙
  3. 金轮法王 小龙女 杨过 慕容复 梅超风 李莫愁 洪七公 张无忌 梅超风 杨逍
  4. 鳌拜 岳不群 黄药师 黄蓉 段誉 金轮法王 忽必烈 忽必烈 张三丰 乔峰 乔峰
  5. 阿紫 乔峰 金轮法王 袁冠南 张无忌 郭襄 黄蓉 李莫愁 赵敏 赵敏 郭芙 张三丰
  6. 乔峰 赵敏 梅超风 双儿 鳌拜 陈家洛 袁冠南 郭芙 郭芙 杨逍 赵敏 金轮法王
  7. 忽必烈 慕容复 张三丰 赵敏 杨逍 令狐冲 黄药师 袁冠南 杨逍 完颜洪烈 殷天正
  8. 李莫愁 阿紫 逍遥子 乔峰 逍遥子 完颜洪烈 郭芙 杨逍 张无忌 杨过 慕容复
  9. 逍遥子 虚竹 双儿 乔峰 郭芙 黄蓉 李莫愁 陈家洛 杨过 忽必烈 鳌拜 王语嫣
  10. 洪七公 韦小宝 阿朱 梅超风 段誉 岳灵珊 完颜洪烈 乔峰 段誉 杨过 杨过 慕容复
  11. 黄蓉 杨过 阿紫 杨逍 张三丰 张三丰 赵敏 张三丰 杨逍 黄蓉 金轮法王 郭襄
  12. 张三丰 令狐冲 赵敏 郭芙 韦小宝 黄药师 阿紫 韦小宝 金轮法王 杨逍 令狐冲 阿紫
  13. 洪七公 袁冠南 双儿 郭靖 鳌拜 谢逊 阿紫 郭襄 梅超风 张无忌 段誉 忽必烈
  14. 完颜洪烈 双儿 逍遥子 谢逊 完颜洪烈 殷天正 金轮法王 张三丰 双儿 郭襄 阿朱
  15. 郭襄 双儿 李莫愁 郭襄 忽必烈 金轮法王 张无忌 鳌拜 忽必烈 郭襄 令狐冲
  16. 谢逊 梅超风 殷天正 段誉 袁冠南 张三丰 王语嫣 阿紫 谢逊 杨过 郭靖 黄蓉
  17. 双儿 灭绝师太 段誉 张无忌 陈家洛 黄蓉 鳌拜 黄药师 逍遥子 忽必烈 赵敏
  18. 逍遥子 完颜洪烈 金轮法王 双儿 鳌拜 洪七公 郭芙 郭襄 赵敏'''
  19. ls = s.split()
  20. d = {}
  21. for i in ls:
  22. d[i] = d.get(i, 0) + 1
  23. max_name, max_cnt = "", 0
  24. for k in d:
  25. if d[k] > max_cnt:
  26. max_name, max_cnt = k, d[k]
  27. print(max_name) # 赵敏

第6章 辅学内容

课前复习

[7.1.1]--前课复习(1).mp4 (48.28MB).mp4%22%2C%22size%22%3A50621704%2C%22taskId%22%3A%22u3481fb69-00cf-46ca-bc5b-fa80dee062b%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480652094-aaac5a22-d505-4371-8ab6-d6e78f04052b.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22SPFYt%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#SPFYt)

本课概要

[7.1.2]--本课概要(1).mp4 (35.51MB).mp4%22%2C%22size%22%3A37234562%2C%22taskId%22%3A%22uea7003db-2d80-4d8e-83c0-b16ea1724d1%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480653521-28e77a0d-eaaf-40e7-9cde-89a9835e1ff9.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22TUgBi%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#TUgBi) 介绍 Python 中 3 种主流组合数据类型的使用方法,掌握处理一组数据的能力:

  • 集合类型
  • 序列类型
  • 字典类型

集合类型及操作

单元开篇

[7.2.1]--单元开篇(1).mp4 (8.17MB).mp4%22%2C%22size%22%3A8564340%2C%22taskId%22%3A%22ue69264f3-2748-4260-b3bd-e961d1f3c31%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480632871-f18f433c-5206-4fc9-aaa7-08ebd078c938.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22lIlpa%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#lIlpa)

集合类型定义

[7.2.2]--集合类型定义(1).mp4 (69.23MB).mp4%22%2C%22size%22%3A72592723%2C%22taskId%22%3A%22ue0b0ab90-df5d-4f1a-a14e-d7b1cfd814b%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480632906-47f5a393-bfd6-40d2-80a5-77da344f2abd.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ZYv7T%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ZYv7T) 集合是多个元素的无序组合

  • 集合类型与数学中的集合概念一致
  • 集合元素是无序的
  • 集合中每个元素唯一,不存在相同元素
  • 集合元素不可更改,不能是可变数据类型
  • 集合用大括号 {} 表示,元素之间用逗号分隔
  • 建立集合类型用 **{}****set()**
  • 建立空集合类型,必须使用 **set()**
  1. A = {"python", 123, ("python", 123)}
  2. B = set("pypy123")
  3. C = {"python", 123, "python",123}
  4. print('A:', A)
  5. print('B:', B)
  6. print('C:', C)
  7. '''
  8. A: {('python', 123), 123, 'python'}
  9. B: {'y', 'p', '2', '1', '3'}
  10. C: {123, 'python'}
  11. '''

集合操作符

[7.2.3]--集合操作符(1).mp4 (65.39MB).mp4%22%2C%22size%22%3A68565704%2C%22taskId%22%3A%22u72a5c24b-7935-4eac-8149-320c6b4f49f%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480632878-0b64b6d5-15a4-4028-99c6-e1c2ce6b0c2e.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22A8i0m%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#A8i0m) 集合类型之间的 4 种运算:
image.png

image.png

image.png

  1. A = {"p", "y", 123}
  2. B = set("pypy123")
  3. print('A =>', A)
  4. print('B =>', B)
  5. print('A - B =>', A - B)
  6. print('A & B =>', A & B)
  7. print('A ^ B =>', A ^ B)
  8. print('B - A =>', B - A)
  9. print('A | B =>', A | B)
  10. '''
  11. A => {'p', 123, 'y'}
  12. B => {'p', '3', '1', '2', 'y'}
  13. A - B => {123}
  14. A & B => {'p', 'y'}
  15. A ^ B => {'3', '1', 123, '2'}
  16. B - A => {'1', '2', '3'}
  17. A | B => {'p', '3', '1', 123, '2', 'y'}
  18. '''

集合处理方法

[7.2.4]--集合处理方法(1).mp4 (102.71MB).mp4%22%2C%22size%22%3A107702783%2C%22taskId%22%3A%22u46ecb504-4941-4964-a670-90e087f2343%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480632895-6a856190-eebc-4b0b-bdc1-b5da2be99a2a.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22Xsagj%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#Xsagj) image.png

image.png

  1. A = {"p", "y", 123}
  2. print('A =>', A)
  3. for item in A:
  4. print(item, end="")
  5. '''
  6. A => {'p', 'y', 123}
  7. py123
  8. '''
  1. A = {"p", "y", 123}
  2. print('A =>', A)
  3. try:
  4. while True:
  5. print(A.pop(), end="")
  6. except:
  7. pass
  8. '''
  9. A => {'p', 'y', 123}
  10. py123
  11. '''

集合类型应用场景

[7.2.5]--集合类型应用场景(1).mp4 (42.22MB).mp4%22%2C%22size%22%3A44270347%2C%22taskId%22%3A%22u955f46e3-6aa9-487a-a889-418619c6d7c%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480632895-976e4b51-a926-4a9b-9e99-87e78ff68e0d.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22k3gJN%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#k3gJN)

  1. print("p" in {"p", "y", 123})
  2. print({"p", "y"} >= {"p", "y", 123})
  3. print({"p", "y"} <= {"p", "y", 123})
  4. '''
  5. True
  6. False
  7. True
  8. '''
  1. ls = ["p", "p", "y", "y", 123]
  2. s = set(ls) # 利用了集合无重复元素的特点
  3. print('s =>', s)
  4. lt = list(s) # 还可以将集合转换为列表
  5. print('lt =>', lt)
  6. '''
  7. s => {123, 'p', 'y'}
  8. lt => [123, 'p', 'y']
  9. '''

单元小结

[7.2.6]--单元小结(1).mp4 (17MB).mp4%22%2C%22size%22%3A17823760%2C%22taskId%22%3A%22uc18f0c8a-2aa4-4585-899f-13e2d0103b3%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480639093-484c94e8-d6a2-40f3-af30-66c0bf2f90d4.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22PwSt7%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#PwSt7)

序列类型及操作

单元开篇

[7.3.1]--单元开篇(1).mp4 (13.3MB).mp4%22%2C%22size%22%3A13949001%2C%22taskId%22%3A%22u304a040c-b870-4143-8412-71b180997b7%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480679393-bc180d08-8978-4152-a7be-f7ff86081b6c.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ndBnR%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ndBnR) 序列类型非常常用,掌握好序列类型,基本上就可以处理绝大多数组合数据类型所需要的应用场景。

序列类型定义

[7.3.2]--序列类型定义(1).mp4 (24.56MB).mp4%22%2C%22size%22%3A25749715%2C%22taskId%22%3A%22udf875daf-bbc8-42c2-b722-6ce1dcb1ed3%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480679386-cd633166-0402-47c2-9082-f8dafdd5bc7f.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22acT5S%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#acT5S) 序列是具有先后关系的一组元素

  • 序列是一维元素向量,元素类型可以不同
  • 类似数学元素序列: s0, s1, … , sn-1
  • 元素间由序号引导,通过下标访问序列的特定元素

序列是一个基类类型,包括:

  • 字符串类型
  • 元组类型
  • 列表类型

image.png

序列处理函数及方法

[7.3.3]--序列处理函数及方法(1).mp4 (68.89MB).mp4%22%2C%22size%22%3A72237816%2C%22taskId%22%3A%22ueedbe1e1-2c80-48dc-a114-86f84b3b533%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480679413-9fa2be26-469f-4789-a0bc-fc4ae2b7b7ab.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ahfFe%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ahfFe) image.png

image.png

  1. ls = ["python", 123, ".io"]
  2. print('ls[::-1] =>', ls[::-1])
  3. print('len(ls) =>', len(ls))
  4. s = 'python123.io'
  5. print('s[::-1] =>', s[::-1])
  6. print('max(s) =>', max(s))
  7. '''
  8. ls[::-1] => ['.io', 123, 'python']
  9. len(ls) => 3
  10. s[::-1] => oi.321nohtyp
  11. max(s) => y
  12. '''

元组类型及操作

[7.3.4]--元组类型及操作(1).mp4 (60.97MB).mp4%22%2C%22size%22%3A63926826%2C%22taskId%22%3A%22ue2915687-618d-4615-85f7-9bbc4ed7e5f%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480679409-e5ac318f-e3ab-4862-8bd0-adc94bfbce37.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22p1bbv%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#p1bbv)

  • 元组是序列类型的一种扩展,它也是一种序列类型
  • 元组类型一旦创建就不能被修改,因此没有针对元组的特殊操作去修改元组类型的值
  • 使用小括号 ()tuple() 创建,元素间用逗号 , 分隔(小括号可以使用也可以不使用)
  • 元组继承了序列类型的全部通用操作(元组本身也是一种序列类型,因此序列能用的元组也 ok)
  1. creature = "car", "dog", "trigger", "human" # 定义元组类型可不加小括号
  2. print("creature =>", creature)
  3. print("creature[::-1] =>", creature[::-1])
  4. print("creature =>", creature)
  5. color = (0x001100, "blue", creature)
  6. print("color =>", color)
  7. print("color[-1][2] =>", color[-1][2])
  8. '''
  9. creature => ('car', 'dog', 'trigger', 'human')
  10. creature[::-1] => ('human', 'trigger', 'dog', 'car')
  11. creature => ('car', 'dog', 'trigger', 'human')
  12. color => (4352, 'blue', ('car', 'dog', 'trigger', 'human'))
  13. color[-1][2] => trigger
  14. '''

列表类型及操作

[7.3.5]--列表类型及操作(1).mp4 (171.73MB).mp4%22%2C%22size%22%3A180071922%2C%22taskId%22%3A%22uddc3f712-43cd-4845-af70-7159a90ecd6%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480679411-7a4deaf5-ae01-42a0-a251-3d9975283966.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ariEh%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ariEh) image.png

image.png

  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. print('ls =>', ls)
  3. lt = ls # 引用赋值
  4. print('lt =>', lt)
  5. lt[0] = 'Cat'
  6. print('ls =>', ls)
  7. print('lt =>', lt)
  8. '''
  9. ls => ['cat', 'dog', 'trigger', 1024]
  10. lt => ['cat', 'dog', 'trigger', 1024]
  11. ls => ['Cat', 'dog', 'trigger', 1024]
  12. lt => ['Cat', 'dog', 'trigger', 1024]
  13. '''
  14. '''
  15. notes:
  16. lt = ls
  17. 这种写法是引用赋值
  18. 意味着 lt、ls 指向的是同一块内存空间
  19. 当我们看到 [] 或者 list() 才意味着新开辟了一块内存
  20. '''
  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. ls[1:2] = [1, 2, 3, 4]
  3. print('ls =>', ls)
  4. del ls[::3]
  5. print('ls =>', ls)
  6. ls = ls * 2
  7. print('ls =>', ls)
  8. '''
  9. ls => ['cat', 1, 2, 3, 4, 'trigger', 1024]
  10. ls => [1, 2, 4, 'trigger']
  11. ls => [1, 2, 4, 'trigger', 1, 2, 4, 'trigger']
  12. '''
  1. ls = ['cat', 'dog', 'trigger', 1024]
  2. ls.append(1234)
  3. print('ls =>', ls)
  4. ls.insert(3, 'human')
  5. print('ls =>', ls)
  6. ls.reverse()
  7. print('ls =>', ls)
  8. '''
  9. ls => ['cat', 'dog', 'trigger', 1024, 1234]
  10. ls => ['cat', 'dog', 'trigger', 'human', 1024, 1234]
  11. ls => [1234, 1024, 'human', 'trigger', 'dog', 'cat']
  12. '''
  1. lt = []
  2. print('1. 定义空列表lt', lt)
  3. lt += [1,2,3,4,5]
  4. print('2. 向lt新增5个元素', lt)
  5. lt[2] = 6
  6. print('3. 修改lt中第2个元素(这里的“第2个”指索引为 2)', lt)
  7. lt.insert(2, 7)
  8. print('4. 向lt中第2个位置增加一个元素', lt)
  9. del lt[1]
  10. print('5. 从lt中第1个位置删除一个元素', lt)
  11. del lt[1:4]
  12. print('6. 删除lt中第1-3位置元素', lt)
  13. rt7 = (0 in lt)
  14. print('7. 判断lt中是否包含数字0', lt, rt7)
  15. lt.append(0)
  16. print('8. 向lt新增数字0', lt)
  17. rt9 = lt.index(0)
  18. print('9. 返回数字0所在lt中的索引', lt, rt9)
  19. rt10 = len(lt)
  20. print('10. lt的长度', rt10)
  21. rt11 = max(lt)
  22. print('11. lt中最大元素', lt, rt11)
  23. lt.clear()
  24. print('12. 清空lt', lt)
  25. '''
  26. 1. 定义空列表lt []
  27. 2. 向lt新增5个元素 [1, 2, 3, 4, 5]
  28. 3. 修改lt中第2个元素(这里的“第2个”指索引为 2) [1, 2, 6, 4, 5]
  29. 4. 向lt中第2个位置增加一个元素 [1, 2, 7, 6, 4, 5]
  30. 5. 从lt中第1个位置删除一个元素 [1, 7, 6, 4, 5]
  31. 6. 删除lt中第1-3位置元素 [1, 5]
  32. 7. 判断lt中是否包含数字0 [1, 5] False
  33. 8. 向lt新增数字0 [1, 5, 0]
  34. 9. 返回数字0所在lt中的索引 [1, 5, 0] 2
  35. 10. lt的长度 3
  36. 11. lt中最大元素 [1, 5, 0] 5
  37. 12. 清空lt []
  38. '''

序列类型应用场景

[7.3.6]--序列类型应用场景(1).mp4 (52.43MB).mp4%22%2C%22size%22%3A54976738%2C%22taskId%22%3A%22ufccfe83f-a121-465e-bee5-5f29c928d5b%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480687768-a23f94e3-1386-4a38-9a30-d91f9eb962c3.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22HCq6z%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#HCq6z) 序列类型数据的表现形式:

  1. 元组
  2. 列表

应用场景

  • 元组用于元素不改变的应用场景,更多用于固定搭配场景
  • 列表更加灵活,它是最常用的序列类型
  • 最主要作用:表示一组有序数据,进而操作它们
  1. for item in ls:
  2. <语句块>
  3. for item in tp:
  4. <语句块>
  1. # 如果不希望数据被程序所改变,转换成元组类型
  2. ls = ['cat', 'dog', 'trigger', 1024]
  3. lt = tuple(ls)
  4. print(lt) # ('cat', 'dog', 'trigger', 1024)

单元小结

[7.3.7]--单元小结(1).mp4 (17.21MB).mp4%22%2C%22size%22%3A18043408%2C%22taskId%22%3A%22u26e98092-efcc-4502-b823-e739cc9fcb2%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480692869-0a73257f-7dff-44bf-aacf-2251e61c4ac8.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22MbxMM%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#MbxMM)

实例9: 基本统计值计算

问题分析

[7.4.1]--基本统计值计算问题分析(1).mp4 (28.21MB).mp4%22%2C%22size%22%3A29584596%2C%22taskId%22%3A%22u9596d22b-0779-46c9-b579-b14be50ec21%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480706661-96b627a2-924b-4af6-bf3b-ea3bb815f16b.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22RmVhX%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#RmVhX) 基本统计值是什么?
image.png

实例讲解

[7.4.2]--基本统计值计算实例讲解(1).mp4 (106.8MB).mp4%22%2C%22size%22%3A111982759%2C%22taskId%22%3A%22u495d6938-af17-4c23-b13c-269bdf8aa18%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480707278-a899b730-bb02-4e7c-8fc6-7ec530f8afbf.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22gmzzX%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#gmzzX)

  1. def getNum(): # 获取用户不定长度的输入
  2. nums = []
  3. iNumStr = input("请输入数字(回车退出): ")
  4. while iNumStr != "":
  5. nums.append(eval(iNumStr))
  6. iNumStr = input("请输入数字(回车退出): ")
  7. return nums
  8. def mean(numbers): # 计算平均值
  9. s = 0.0
  10. for num in numbers:
  11. s = s + num
  12. return s / len(numbers)
  13. def dev(numbers, mean): # 计算方差
  14. sdev = 0.0
  15. for num in numbers:
  16. sdev = sdev + (num - mean)**2
  17. return pow(sdev / (len(numbers)-1), 0.5)
  18. def median(numbers): # 计算中位数
  19. sorted(numbers)
  20. size = len(numbers)
  21. if size % 2 == 0:
  22. med = (numbers[size//2-1] + numbers[size//2])/2
  23. else:
  24. med = numbers[size//2]
  25. return med
  26. n = getNum() # 主体函数
  27. m = mean(n)
  28. print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n, m), median(n)))
  29. '''
  30. 请输入数字(回车退出): 10
  31. 请输入数字(回车退出): 20
  32. 请输入数字(回车退出): 30
  33. 请输入数字(回车退出):
  34. 平均值:20.0,方差:1e+01,中位数:20.
  35. '''

举一反三

[7.4.3]--基本统计值计算举一反三(1).mp4 (34.84MB).mp4%22%2C%22size%22%3A36532762%2C%22taskId%22%3A%22ufecd3725-54a7-4d63-acd8-85e563779ae%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480710327-fbb87d79-27a0-4618-a7e0-42243cd55cca.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22mzVI7%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#mzVI7) 通过本节介绍的实例,掌握以下几点:

  • 获取多个数据:从控制台获取多个不确定数据的方法
  • 分隔多个函数:模块化设计方法
  • 充分利用函数:充分利用 Python 提供的内置函数

字典类型及操作

单元开篇

[7.5.1]--单元开篇(1).mp4 (9.17MB).mp4%22%2C%22size%22%3A9612023%2C%22taskId%22%3A%22ud248f45f-1072-46f5-95dd-496405a06a0%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480712626-2ca88088-fddd-49f3-9c69-a8185cda797a.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22fQcaV%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#fQcaV)

字典类型定义

[7.5.2]--字典类型定义(1).mp4 (95.36MB).mp4%22%2C%22size%22%3A99996662%2C%22taskId%22%3A%22u17b7bcf5-7264-41db-89ba-b8b1f71bdca%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480720721-adcf5c19-823f-4a00-97f4-b24ab956d15d.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22CBN6T%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#CBN6T) 理解“映射”:映射是一种键(索引)和值(数据)的对应
image.png
image.png
image.png

字典类型是“映射”的体现,在字典变量中,通过键获得值

  • 键值对:键是数据索引的扩展
  • 字典是键值对的集合,键值对之间无序
  • 采用大括号 {}dict() 创建,键值对用冒号 : 表示
  1. <字典变量> = {
  2. <键1>:<值1>,
  3. <键2>:<值2>,
  4. …,
  5. <键n>:<值n>
  6. }
  7. <值1> = <字典变量1>[<键1>]
  8. <值2> = <字典变量1>[<键2>]
  9. <值n> = <字典变量1>[<键n>]
  10. # 中括号 [ ] 用来向字典变量中索引或增加元素
  1. d = {"中国": "北京", "美国": "华盛顿", "法国": "巴黎"}
  2. print("d =>", d)
  3. print('d["中国"] =>', d["中国"])
  4. print('type(d) =>', type(d))
  5. '''
  6. d => {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  7. d["中国"] => 北京
  8. type(d) => <class 'dict'>
  9. '''

字典处理函数及方法

[7.5.3]--字典处理函数及方法(1).mp4 (101.67MB).mp4%22%2C%22size%22%3A106611247%2C%22taskId%22%3A%22u7a65b52d-479b-4e06-8776-507441b496a%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480720931-8d44ea09-e16d-46f3-9db8-7cacdeeeb9fc.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22qAnsV%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#qAnsV) image.png

image.png

  1. d = {"中国": "北京", "美国": "华盛顿", "法国": "巴黎"}
  2. print('1.', "中国" in d)
  3. print('2.', d.keys())
  4. print('3.', d.values())
  5. print('4.', d.get("中国", "伊斯兰堡"))
  6. print('5.', d)
  7. print('6.', d.get("巴基斯坦", "伊斯兰堡"))
  8. print('7.', d)
  9. print('8.', d.popitem())
  10. print('9.', d)
  11. '''
  12. 1. True
  13. 2. dict_keys(['中国', '美国', '法国'])
  14. 3. dict_values(['北京', '华盛顿', '巴黎'])
  15. 4. 北京
  16. 5. {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  17. 6. 伊斯兰堡
  18. 7. {'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
  19. 8. ('法国', '巴黎')
  20. 9. {'中国': '北京', '美国': '华盛顿'}
  21. '''
  1. # 1. 定义空字典d
  2. d = {}
  3. print("1.", d)
  4. # 2. 向d新增2个键值对元素
  5. d["a"] = 1
  6. d["b"] = 2
  7. print("2.", d)
  8. # 3. 修改第2个添加元素
  9. d["b"] = 3
  10. print("3.", d)
  11. # 4. 判断字符"c"是否是d的键
  12. "c" in d
  13. print("4.", "c" in d)
  14. # 5. 计算d的长度
  15. len(d)
  16. print("5.", len(d))
  17. # 6. 清空d
  18. d.clear()
  19. print('6.', d)
  20. """
  21. 1. {}
  22. 2. {'a': 1, 'b': 2}
  23. 3. {'a': 1, 'b': 3}
  24. 4. False
  25. 5. 2
  26. 6. {}
  27. """

字典类型应用场景

[7.5.4]--字典类型应用场景(1).mp4 (22.19MB).mp4%22%2C%22size%22%3A23264812%2C%22taskId%22%3A%22u9fbc30e6-cacb-48f6-ba5f-37c4aba4f56%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480728249-ede627fa-64cd-4182-bfbe-a11b67e5d10d.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22exJ4d%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#exJ4d) 字典类型的应用场景:映射的表达

  • 映射无处不在,键值对无处不在
  • 例如:统计数据出现的次数,数据是键,次数是值
  • 最主要作用:表达键值对数据,进而操作它们
  1. for k in d:
  2. <语句块>

本周介绍的集合类型,主要解决的问题就是对一组数据的处理,我们需要掌握:如何合理地使用集合类型来表达一组数据,并管理这一组数据。

单元小结

[7.5.5]--单元小结(1).mp4 (15.85MB).mp4%22%2C%22size%22%3A16620534%2C%22taskId%22%3A%22udab0e2f3-6b5a-48e5-9f53-edb98f7d249%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480744413-43b8d574-ba0f-4b09-95e4-20a7164dd20c.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22eCuy5%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#eCuy5)

  • 映射关系采用键值对表达
  • 字典类型使用 {}dict() 创建,键值对之间用 : 分隔
  • d[key] 方式既可以索引,也可以赋值
  • 字典类型有一批操作方法和函数,最重要的是 .get()

模块5: jieba 库的使用

jieba 库基本介绍

[7.6.1]--jieba库基本介绍(1).mp4 (33.49MB).mp4%22%2C%22size%22%3A35116866%2C%22taskId%22%3A%22u67d60dee-d0cb-4fe5-974e-a0a2a392c94%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480749383-205c0547-edd1-4f5c-84da-9a4298033816.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22eae00%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#eae00) jieba 是优秀的中文分词第三方库

  • 中文文本需要通过分词获得单个的词语
  • jieba是优秀的中文分词第三方库,需要额外安装
  • jieba库提供三种分词模式,最简单只需掌握一个函数

jieba 库的安装:pip install jieba

jieba 分词的原理:jieba 分词依靠中文词库

  • 利用一个中文词库,确定中文字符之间的关联概率
  • 中文字符间概率大的组成词组,形成分词结果
  • 除了分词,用户还可以添加自定义的词组

jieba 库使用说明

[7.6.2]--jieba库使用说明(1).mp4 (66.54MB).mp4%22%2C%22size%22%3A69777454%2C%22taskId%22%3A%22udfcd8b1d-8d9d-4e9c-a5de-22c66f79c7c%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480752476-fd903ab3-c520-4607-8c63-bb9b2ae0877a.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22cT3v2%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#cT3v2) jieba 分词的三种模式:精确模式、全模式、搜索引擎模式

  • 精确模式:把文本精确的切分开,不存在冗余单词
  • 全模式:把文本中所有可能的词语都扫描出来,有冗余
  • 搜索引擎模式:在精确模式基础上,对长词再次切分

image.png
image.png

重点:jieba.lcut(s)

实例10: 文本词频统计

问题分析

[7.7.1]--文本词频统计问题分析(1).mp4 (19.54MB).mp4%22%2C%22size%22%3A20491334%2C%22taskId%22%3A%22u8ab8b573-f7e0-4985-88e4-1a67d929f47%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480753746-808f0d8f-a9d9-45f5-bce3-a96b4e7bd2b8.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22qa8re%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#qa8re) 文本词频统计

需求:一篇文章,出现了哪些词?哪些词出现得最多?

该怎么做呢?
先分析文本是英文文本还是中文文本

素材:

实例讲解

[7.7.2]--Hamlet英文词频统计实例讲解(1).mp4 (118.09MB).mp4%22%2C%22size%22%3A123828580%2C%22taskId%22%3A%22ub7bb16ce-c4fd-425e-b399-a2b711badbe%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480762044-984b48e3-2a54-4660-aa3e-4e20fd7ab179.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22Zkw7Q%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#Zkw7Q)

  1. def getText():
  2. txt = open("hamlet.txt", "r").read()
  3. txt = txt.lower()
  4. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  5. txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
  6. return txt
  7. hamletTxt = getText()
  8. words = hamletTxt.split()
  9. counts = {}
  10. for word in words:
  11. counts[word] = counts.get(word, 0) + 1
  12. items = list(counts.items())
  13. items.sort(key=lambda x: x[1], reverse=True)
  14. for i in range(10):
  15. word, count = items[i]
  16. print("{0:<10}{1:>5}".format(word, count))
  17. '''
  18. the 1138
  19. and 965
  20. to 754
  21. of 669
  22. you 550
  23. i 542
  24. a 542
  25. my 514
  26. hamlet 462
  27. in 436
  28. '''

这是一个简单的词频统计程序,用于分析《哈姆雷特》剧本的文本数据。下面是每一部分的详细解释:

  1. getText 函数读取存储在 “hamlet.txt” 文件中的哈姆雷特文本。它将所有字符转换为小写,然后将所有特殊字符(如标点符号和其他非字母数字字符)替换为空格。
  2. hamletTxt 是从 “hamlet.txt” 文件中获取的处理过的文本。
  3. words 是一个列表,其中包含 hamletTxt 中的所有单词。单词是通过将文本字符串 hamletTxt 按空格字符分割得到的。
  4. counts 是一个字典,用于存储每个单词及其出现的次数。
  5. 接下来的 for 循环遍历 words 列表中的每个单词。对于列表中的每个单词,如果它不在 counts 字典中,就将它添加到字典中并设置其计数为1。如果它已经在字典中,就将其计数增加1。
  6. 然后,将 counts 字典转换为一个包含键值对(单词和计数)的列表 items
  7. 通过 sort 方法对 items 列表进行排序,按照每个元素的第二个值(即单词出现的次数)进行降序排序。
  8. 最后,打印出现频率最高的前10个单词及其出现次数。

整体来说,该程序的目的是找出哈姆雷特文本中最常出现的单词。

实例讲解(上)

[7.7.3]--《三国演义》人物出场统计实例讲解(上)(1).mp4 (45.5MB)(1).mp4%22%2C%22size%22%3A47706817%2C%22taskId%22%3A%22ua2538291-6dd9-4dcf-87da-8d3a92fc3b0%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480764141-792e9ac7-7381-4e49-8612-0cea0f9a1a1b.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22j16yo%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#j16yo)

  1. import jieba
  2. txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
  3. words = jieba.lcut(txt)
  4. counts = {}
  5. for word in words:
  6. if len(word) == 1:
  7. continue
  8. else:
  9. counts[word] = counts.get(word,0) + 1
  10. items = list(counts.items())
  11. items.sort(key=lambda x:x[1], reverse=True)
  12. for i in range(15):
  13. word, count = items[i]
  14. print ("{0:<10}{1:>5}".format(word, count))
  15. '''
  16. 曹操 953
  17. 孔明 836
  18. 将军 772
  19. 却说 656
  20. 玄德 585
  21. 关公 510
  22. 丞相 491
  23. 二人 469
  24. 不可 440
  25. 荆州 425
  26. 玄德曰 390
  27. 孔明曰 390
  28. 不能 384
  29. 如此 378
  30. 张飞 358
  31. '''

这个程序是用于分析《三国演义》文本的词频统计程序,它基于 Python 的 jieba 模块,这是一个用于中文分词的库。下面是每一部分的详细解释:

  1. 首先,程序打开 “threekingdoms.txt” 文件并读取所有的文本内容。该文件应该包含《三国演义》的全部或部分文本。
  2. 然后使用 jieba.lcut() 函数将全文进行分词。分词的结果是一个列表,其中每个元素都是一个单词或短语。
  3. counts 是一个字典,用于存储每个词及其出现的次数。
  4. 程序通过 for 循环遍历 words 列表中的每个词。如果一个词的长度为1(即,它是一个单个的中文字符),则程序将跳过这个词并继续下一个词。否则,程序将检查这个词是否已经存在于 counts 字典中。如果不存在,将它添加到字典中并设置其计数为1。如果存在,就将其计数增加1。
  5. 然后,将 counts 字典转换为一个包含键值对(即词和计数)的列表 items
  6. 接下来,使用 sort() 函数对 items 列表进行排序,排序的依据是每个元素的第二个值(即词出现的次数)。reverse=True 指定按降序排序。
  7. 最后,打印出现频率最高的前15个词及其出现次数。

总的来说,该程序的目的是找出《三国演义》中最常出现的词或短语。这个程序利用了 jieba 库的中文分词能力,所以它能够处理复杂的中文文本数据。

实例讲解(下)

[7.7.4]--《三国演义》人物出场统计实例讲解(下)(1).mp4 (96.76MB)(1).mp4%22%2C%22size%22%3A101458861%2C%22taskId%22%3A%22ufd2c18cb-87ec-4bb2-b743-f36c5f71622%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480764180-d742a8b5-2cbf-4424-b3c8-3c70fc59c299.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22py7f0%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#py7f0) image.png

  1. import jieba
  2. excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此"}
  3. txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
  4. words = jieba.lcut(txt)
  5. counts = {}
  6. for word in words:
  7. if len(word) == 1:
  8. continue
  9. elif word == "诸葛亮" or word == "孔明曰":
  10. rword = "孔明"
  11. elif word == "关公" or word == "云长":
  12. rword = "关羽"
  13. elif word == "玄德" or word == "玄德曰":
  14. rword = "刘备"
  15. elif word == "孟德" or word == "丞相":
  16. rword = "曹操"
  17. else:
  18. rword = word
  19. counts[rword] = counts.get(rword, 0) + 1
  20. for word in excludes:
  21. del counts[word]
  22. items = list(counts.items())
  23. items.sort(key=lambda x: x[1], reverse=True)
  24. for i in range(10):
  25. word, count = items[i]
  26. print("{0:<10}{1:>5}".format(word, count))
  27. '''
  28. 曹操 1451
  29. 孔明 1383
  30. 刘备 1252
  31. 关羽 784
  32. 张飞 358
  33. 商议 344
  34. 如何 338
  35. 主公 331
  36. 军士 317
  37. 吕布 300
  38. '''

这个程序是对 “CalThreeKingdomsV1.py” 的改进。它同样用于分析《三国演义》文本的词频统计,但增加了更多的功能。

  1. 程序首先定义了一个 excludes 集合,这个集合包含一些我们想要从统计结果中排除的词。
  2. 然后,程序打开 “threekingdoms.txt” 文件并读取所有的文本内容。该文件应该包含《三国演义》的全部或部分文本。
  3. 使用 jieba.lcut() 函数将全文进行分词。分词的结果是一个列表,其中每个元素都是一个单词或短语。
  4. counts 是一个字典,用于存储每个词及其出现的次数。
  5. 程序通过 for 循环遍历 words 列表中的每个词。如果一个词的长度为1(即,它是一个单个的中文字符),则程序将跳过这个词并继续下一个词。程序对一些特殊的词进行处理,比如将 “诸葛亮” 和 “孔明曰” 都视为 “孔明”。这样做的目的是处理文本中的同一实体可能有多种表示方法的问题。然后,程序将检查这个词(或者处理过的词)是否已经存在于 counts 字典中。如果不存在,将它添加到字典中并设置其计数为1。如果存在,就将其计数增加1。
  6. 在统计完所有词的出现次数之后,程序遍历 excludes 集合中的每一个词,并从 counts 字典中删除它们。这是为了在最后的统计结果中排除这些词。
  7. 然后,将 counts 字典转换为一个包含键值对(即词和计数)的列表 items
  8. 使用 sort() 函数对 items 列表进行排序,排序的依据是每个元素的第二个值(即词出现的次数)。reverse=True 指定按降序排序。
  9. 最后,打印出现频率最高的前10个词及其出现次数。

总的来说,该程序的目的是找出《三国演义》中最常出现的词或短语(排除一些常见但并不重要的词)。这个程序利用了 jieba 库的中文分词能力,并进行了更复杂的处理,以便更准确地分析中文文本。

举一反三

[7.7.5]--文本词频统计举一反三(1).mp4 (26.34MB).mp4%22%2C%22size%22%3A27621144%2C%22taskId%22%3A%22u385b113a-0727-4de6-bd9a-b3ecdea9371%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480764249-d4d491e8-5447-4c29-ae20-ea3ecb114e9c.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22FffKs%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#FffKs) image.png

作业与练习

[7.8.1]--练习与作业(1).mp4 (4.4MB).mp4%22%2C%22size%22%3A4608993%2C%22taskId%22%3A%22u98fddeb5-77c4-4d9f-883d-88594825beb%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688480780756-cf0a7fa1-b998-4409-a8f5-2eae343ce740.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22FT3pM%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#FT3pM)

单选题

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

人名独特性统计

image.png

  1. s = '''双儿 洪七公 赵敏 赵敏 逍遥子 鳌拜 殷天正 金轮法王 乔峰 杨过 洪七公 郭靖
  2. 杨逍 鳌拜 殷天正 段誉 杨逍 慕容复 阿紫 慕容复 郭芙 乔峰 令狐冲 郭芙
  3. 金轮法王 小龙女 杨过 慕容复 梅超风 李莫愁 洪七公 张无忌 梅超风 杨逍
  4. 鳌拜 岳不群 黄药师 黄蓉 段誉 金轮法王 忽必烈 忽必烈 张三丰 乔峰 乔峰
  5. 阿紫 乔峰 金轮法王 袁冠南 张无忌 郭襄 黄蓉 李莫愁 赵敏 赵敏 郭芙 张三丰
  6. 乔峰 赵敏 梅超风 双儿 鳌拜 陈家洛 袁冠南 郭芙 郭芙 杨逍 赵敏 金轮法王
  7. 忽必烈 慕容复 张三丰 杨逍 令狐冲 黄药师 袁冠南 杨逍 完颜洪烈 殷天正
  8. 李莫愁 阿紫 逍遥子 乔峰 逍遥子 完颜洪烈 郭芙 杨逍 张无忌 杨过 慕容复
  9. 逍遥子 虚竹 双儿 乔峰 郭芙 黄蓉 李莫愁 陈家洛 杨过 忽必烈 鳌拜 王语嫣
  10. 洪七公 韦小宝 阿朱 梅超风 段誉 岳灵珊 完颜洪烈 乔峰 段誉 杨过 杨过 慕容复
  11. 黄蓉 杨过 阿紫 杨逍 张三丰 张三丰 赵敏 张三丰 杨逍 黄蓉 金轮法王 郭襄
  12. 张三丰 令狐冲 郭芙 韦小宝 黄药师 阿紫 韦小宝 金轮法王 杨逍 令狐冲 阿紫
  13. 洪七公 袁冠南 双儿 郭靖 鳌拜 谢逊 阿紫 郭襄 梅超风 张无忌 段誉 忽必烈
  14. 完颜洪烈 双儿 逍遥子 谢逊 完颜洪烈 殷天正 金轮法王 张三丰 双儿 郭襄 阿朱
  15. 郭襄 双儿 李莫愁 郭襄 忽必烈 金轮法王 张无忌 鳌拜 忽必烈 郭襄 令狐冲
  16. 谢逊 梅超风 殷天正 段誉 袁冠南 张三丰 王语嫣 阿紫 谢逊 杨过 郭靖 黄蓉
  17. 双儿 灭绝师太 段誉 张无忌 陈家洛 黄蓉 鳌拜 黄药师 逍遥子 忽必烈 赵敏
  18. 逍遥子 完颜洪烈 金轮法王 双儿 鳌拜 洪七公 郭芙 郭襄'''
  19. ls = s.split()
  20. ss = set(ls)
  21. print(len(ss)) # 36

去重需求由集合类型来完成。

字典翻转输出

image.png

  1. s = input()
  2. try:
  3. d = eval(s)
  4. e = {}
  5. for k in d:
  6. e[d[k]] = k
  7. print(e)
  8. except:
  9. print("输入错误")
  10. '''
  11. {"a": 1, "b": 2}
  12. {1: 'a', 2: 'b'}
  13. '''

《沉默的羔羊》之最多单词

image.png

附件:沉默的羔羊.txt

  1. import jieba
  2. f = open("沉默的羔羊.txt", encoding='utf-8')
  3. ls = jieba.lcut(f.read())
  4. d = {}
  5. for w in ls:
  6. if len(w) >= 2:
  7. d[w] = d.get(w, 0) + 1
  8. maxc = 0
  9. maxw = ""
  10. for k in d:
  11. if d[k] > maxc:
  12. maxc = d[k]
  13. maxw = k
  14. elif d[k] == maxc and k > maxw:
  15. maxw = k
  16. print(maxw) # 史达琳
  17. f.close()

数字不同数之和

image.png

  1. n = input()
  2. ss = set(n)
  3. s = 0
  4. for i in ss:
  5. s += eval(i)
  6. print(s)
  7. '''
  8. 123123123
  9. 6
  10. '''

注意,字符串可以通过list()直接变成列表,或通过set()直接变成集合。

人名最多数统计

image.png

  1. s = '''双儿 洪七公 赵敏 赵敏 逍遥子 鳌拜 殷天正 金轮法王 乔峰 杨过 洪七公 郭靖
  2. 杨逍 鳌拜 殷天正 段誉 杨逍 慕容复 阿紫 慕容复 郭芙 乔峰 令狐冲 郭芙
  3. 金轮法王 小龙女 杨过 慕容复 梅超风 李莫愁 洪七公 张无忌 梅超风 杨逍
  4. 鳌拜 岳不群 黄药师 黄蓉 段誉 金轮法王 忽必烈 忽必烈 张三丰 乔峰 乔峰
  5. 阿紫 乔峰 金轮法王 袁冠南 张无忌 郭襄 黄蓉 李莫愁 赵敏 赵敏 郭芙 张三丰
  6. 乔峰 赵敏 梅超风 双儿 鳌拜 陈家洛 袁冠南 郭芙 郭芙 杨逍 赵敏 金轮法王
  7. 忽必烈 慕容复 张三丰 赵敏 杨逍 令狐冲 黄药师 袁冠南 杨逍 完颜洪烈 殷天正
  8. 李莫愁 阿紫 逍遥子 乔峰 逍遥子 完颜洪烈 郭芙 杨逍 张无忌 杨过 慕容复
  9. 逍遥子 虚竹 双儿 乔峰 郭芙 黄蓉 李莫愁 陈家洛 杨过 忽必烈 鳌拜 王语嫣
  10. 洪七公 韦小宝 阿朱 梅超风 段誉 岳灵珊 完颜洪烈 乔峰 段誉 杨过 杨过 慕容复
  11. 黄蓉 杨过 阿紫 杨逍 张三丰 张三丰 赵敏 张三丰 杨逍 黄蓉 金轮法王 郭襄
  12. 张三丰 令狐冲 赵敏 郭芙 韦小宝 黄药师 阿紫 韦小宝 金轮法王 杨逍 令狐冲 阿紫
  13. 洪七公 袁冠南 双儿 郭靖 鳌拜 谢逊 阿紫 郭襄 梅超风 张无忌 段誉 忽必烈
  14. 完颜洪烈 双儿 逍遥子 谢逊 完颜洪烈 殷天正 金轮法王 张三丰 双儿 郭襄 阿朱
  15. 郭襄 双儿 李莫愁 郭襄 忽必烈 金轮法王 张无忌 鳌拜 忽必烈 郭襄 令狐冲
  16. 谢逊 梅超风 殷天正 段誉 袁冠南 张三丰 王语嫣 阿紫 谢逊 杨过 郭靖 黄蓉
  17. 双儿 灭绝师太 段誉 张无忌 陈家洛 黄蓉 鳌拜 黄药师 逍遥子 忽必烈 赵敏
  18. 逍遥子 完颜洪烈 金轮法王 双儿 鳌拜 洪七公 郭芙 郭襄 赵敏'''
  19. ls = s.split()
  20. d = {}
  21. for i in ls:
  22. d[i] = d.get(i, 0) + 1
  23. max_name, max_cnt = "", 0
  24. for k in d:
  25. if d[k] > max_cnt:
  26. max_name, max_cnt = k, d[k]
  27. print(max_name) # 赵敏

这是传统解法,先使用字典建立”姓名与出现次数”的关系,然后找出现次数最多数对应的姓名。

补充

基本统计值是什么?

在编程中,“基本统计值”(basic statistical measures)是指用于描述和分析数据集的一组常见统计量。这些统计量提供了有关数据集中数据分布、中心趋势和变异程度的信息,有助于了解数据的特征和性质

以下是一些常见的基本统计值:

  1. 平均值(Mean):数据集中所有数值的总和除以数据的数量,用于衡量数据的中心趋势。
  2. 中位数(Median):将数据集中的数值按升序排列,取中间位置的数值作为中位数,用于衡量数据的中心趋势。
  3. 众数(Mode):数据集中出现次数最多的数值,可能有一个或多个众数。
  4. 标准差(Standard Deviation):衡量数据的离散程度或变异程度,表示数据值与其平均值之间的差异。
  5. 范围(Range):数据集中最大值与最小值之间的差异,用于衡量数据的变化范围。
  6. 四分位数(Quartiles):将数据集按升序排列后,将其划分为四个等分点,分别是第一四分位数(25%分位数)、第二四分位数(中位数,50%分位数)和第三四分位数(75%分位数)。

这些基本统计值可以提供对数据集的总体概述和特征描述,有助于数据分析、可视化和做出决策。

需要注意的是,基本统计值只是数据分析中的一部分,还有其他更高级的统计方法和概念可以应用于数据集。

为什么嵩老师不建议低龄儿童学编程?

image.png

全国计算机等级考试二级 Python 科目

image.png