5.2 条件测试,遍历字典里面的value值

字典里面的value值(为列表)的遍历

测试特殊值(100)是否在里面

测试条件(》=100分的),输出其名字

 

"""
  多个条件测试
  遍历字典的value值
"""
cjs = {'侯世豪': [102, 87, 114, 39, 75, 53], '徐佳怡': [105, 105, 98, 32, 67, 58], '刘晨曦': [100, 95, 98, 50, 54, 58],
       '刘珈岩': [88, 100, 139, 30, 34, 56], '张梓浩': [90, 103, 122, 35, 46, 45], '李海涛': [84, 90, 95, 43, 63, 62],
       '王文哲': [90, 88, 72, 66, 71, 44], '白雨含': [99, 74, 110, 51, 41, 47], '赵彦': [99, 88, 98, 33, 49, 54],
       '林鑫浩': [85, 94, 80, 30, 68, 54], '王英男': [86, 81, 106, 23, 61, 49], '安姿沅': [103, 91, 90, 31, 50, 40],
       '闫璐璐': [87, 80, 106, 43, 39, 49], '尚之惠': [75, 84, 85, 48, 71, 40], '郭璨': [97, 82, 82, 38, 52, 47],
       '李典典': [105, 88, 106, 34, 22, 42], '刘鹏程': [91, 79, 84, 36, 51, 54], '周泽宇': [67, 105, 79, 35, 59, 47],
       '宁致远': [83, 89, 85, 49, 38, 48], '吕子涵': [98, 66, 83, 57, 34, 53], '赵育梁': [77, 95, 63, 58, 36, 60],
       '王祎玮': [104, 53, 117, 36, 33, 36], '赵烁琬': [90, 92, 78, 39, 30, 45], '李静雯': [98, 72, 96, 30, 35, 30],
       '刘培铭': [87, 70, 73, 24, 55, 51], '王炳雨': [87, 71, 77, 36, 41, 43], '杨淑姣': [83, 60, 101, 23, 30, 55],
       '谢仕隆': [43, 95, 78, 42, 45, 42], '郭佳': [93, 62, 91, 22, 35, 40], '胡馨怡': [104, 57, 74, 32, 19, 55],
       '陈璐': [88, 58, 83, 19, 32, 46], '马泰': [51, 103, 59, 30, 28, 50], '高源': [78, 84, 61, 16, 36, 42],
       '周雨萌': [74, 75, 86, 23, 35, 21], '王雨轩': [50, 73, 79, 36, 33, 38], '朱鹏霖': [63, 74, 20, 40, 56, 46],
       '李威': [47, 89, 26, 24, 24, 28]}
names = []
names1 = []
for records in cjs:
    if 105 in cjs.get(records):
        names.append(records)
for name in names:
    print(f"{name} ,you have a 105 score.")
for records in cjs:  # 遍历字典里面的value值
    for cj in cjs.get(records):
        if cj >= 100:
            names1.append(records)
names1 = set(names1)
for name in names1:
    print(f"{name} ,you are great.")

 效果:

成绩为105分的学生有3个.他们是:
周泽宇 ,you have a 105 score. 徐佳怡 ,you have a 105 score. 李典典 ,you have a 105 score.
有一科目成绩大于100分的学生有15个.他们是:
{'闫璐璐', '白雨含', '周泽宇', '王英男', '侯世豪', '王祎玮', '胡馨怡', '刘晨曦', '张梓浩', '杨淑姣', '安姿沅', '马泰', '徐佳怡', '刘珈岩', '李典典'}
闫璐璐 ,you are great. 白雨含 ,you are great. 周泽宇 ,you are great. 王英男 ,you are great. 侯世豪 ,you are great. 王祎玮 ,you are great. 胡馨怡 ,you are great. 刘晨曦 ,you are great. 张梓浩 ,you are great. 杨淑姣 ,you are great. 安姿沅 ,you are great. 马泰 ,you are great. 徐佳怡 ,you are great. 刘珈岩 ,you are great. 李典典 ,you are great.
进程已结束,退出代码0

总结:

字典里面的数据默认的是key,也就是records是从keys里面取出来

可以将列表转换成集合进行去重

 1 """
 2 多个条件测试
 3 遍历字典的value值
 4 """
 5 cjs = {'侯世豪': [102, 87, 114, 39, 75, 53], '徐佳怡': [105, 105, 98, 32, 67, 58], '刘晨曦': [100, 95, 98, 50, 54, 58], '刘珈岩': [88, 100, 139, 30, 34, 56], '张梓浩': [90, 103, 122, 35, 46, 45], '李海涛': [84, 90, 95, 43, 63, 62], '王文哲': [90, 88, 72, 66, 71, 44], '白雨含': [99, 74, 110, 51, 41, 47], '赵彦': [99, 88, 98, 33, 49, 54], '林鑫浩': [85, 94, 80, 30, 68, 54], '王英男': [86, 81, 106, 23, 61, 49], '安姿沅': [103, 91, 90, 31, 50, 40], '闫璐璐': [87, 80, 106, 43, 39, 49], '尚之惠': [75, 84, 85, 48, 71, 40], '郭璨': [97, 82, 82, 38, 52, 47], '李典典': [105, 88, 106, 34, 22, 42], '刘鹏程': [91, 79, 84, 36, 51, 54], '周泽宇': [67, 105, 79, 35, 59, 47], '宁致远': [83, 89, 85, 49, 38, 48], '吕子涵': [98, 66, 83, 57, 34, 53], '赵育梁': [77, 95, 63, 58, 36, 60], '王祎玮': [104, 53, 117, 36, 33, 36], '赵烁琬': [90, 92, 78, 39, 30, 45], '李静雯': [98, 72, 96, 30, 35, 30], '刘培铭': [87, 70, 73, 24, 55, 51], '王炳雨': [87, 71, 77, 36, 41, 43], '杨淑姣': [83, 60, 101, 23, 30, 55], '谢仕隆': [43, 95, 78, 42, 45, 42], '郭佳': [93, 62, 91, 22, 35, 40], '胡馨怡': [104, 57, 74, 32, 19, 55], '陈璐': [88, 58, 83, 19, 32, 46], '马泰': [51, 103, 59, 30, 28, 50], '高源': [78, 84, 61, 16, 36, 42], '周雨萌': [74, 75, 86, 23, 35, 21], '王雨轩': [50, 73, 79, 36, 33, 38], '朱鹏霖': [63, 74, 20, 40, 56, 46], '李威': [47, 89, 26, 24, 24, 28]}
 6 names = []
 7 names1 = []
 8 for records in cjs:
 9     for record in cjs.get(records):
10         if record == 105:
11             names.append(records)
12         elif record >= 100:
13             names1.append(records)
14 names = set(names)
15 names1 = set(names1)
16 print(f'成绩为105分的学生有{len(names)}个.他们是:')
17 for name in names:
18     print(f"{name} ,you have a 105 score.",end='\t')
19 print(f"\n有一科目成绩大于100分的学生有{len(names1)}个.他们是:")
20 for name in names1:
21     print(f"{name} ,you are great.",end='\t')

 

效果:

 成绩为105分的学生有3个.他们是:

李典典 ,you have a 105 score. 周泽宇 ,you have a 105 score. 徐佳怡 ,you have a 105 score.
有一科目成绩大于100分的学生有13个.他们是:
{'侯世豪', '胡馨怡', '白雨含', '刘珈岩', '安姿沅', '王祎玮', '刘晨曦', '李典典', '马泰', '张梓浩', '闫璐璐', '王英男', '杨淑姣'}
侯世豪 ,you are great. 胡馨怡 ,you are great. 白雨含 ,you are great. 刘珈岩 ,you are great. 安姿沅 ,you are great. 王祎玮 ,you are great. 刘晨曦 ,you are great. 李典典 ,you are great. 马泰 ,you are great. 张梓浩 ,you are great. 闫璐璐 ,you are great. 王英男 ,you are great. 杨淑姣 ,you are great.
进程已结束,退出代码0

 问题:

# 当这个 布尔表达式 为真,105分对应的名字添加进names中,进行列表后面的判断了,105分不会再与100分进行比较了
改进:
1 for records in cjs:
2     for record in cjs.get(records):
3         if record >= 100:
4             names1.append(records)
5             if record == 105:
6                 names.append(records)

 

 这样105执行完第11行后继续执行下面的12,13行。

又改进,记录成长:

1 for records in cjs:
2     for record in cjs.get(records):
3         if record >= 100:
4             if records not in names1:   # 增加判断,若不在列表中,添加
5                 names1.append(records)
6             if record == 105:
7                 if records not in names:
8                     names.append(records)

 

所以条件测试与程序执行的顺序一定要弄好,要多测试才能更好地理解布尔表达式。

posted @ 2022-07-28 13:16  scholar-for-ever  阅读(108)  评论(0编辑  收藏  举报