Python获取指定key字段下的所有值、根据指定key字段筛选和删除数据记录

您所在的位置:网站首页 python取字典中的值 Python获取指定key字段下的所有值、根据指定key字段筛选和删除数据记录

Python获取指定key字段下的所有值、根据指定key字段筛选和删除数据记录

2024-01-23 16:54| 来源: 网络整理| 查看: 265

当我们一次性获得众多数据时,相当于字典数组,如所有的学生科目成绩时,有时候会有需要对指定key字段进行数据统计等操作,或根据指定key字段进行数据的筛选或删除(相当于sql中的where操作)

无独有偶,我这边在应用的时候,因为是将获取到的所有字典数组,不经过处理,全部放到数组中,形成的字典数组。此时需要对字典数组进行数据的筛选、删除、统计等操作。

以下用学生科目成绩,分别对统计、筛选、删除,以及对应的组合操作,做对应示例:

1、获取字典数组中,指定key字段下的所有值,并加以统计操作

stus = [{'name': '小王', 'age': 14, 'chinese': 81, 'math': 92, 'english': 80}, {'name': '小刘', 'age': 13, 'chinese': 85, 'math': 77, 'english': 83}, {'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小吴', 'age': 12, 'chinese': 78, 'math': 71, 'english': 74}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # 获取所有chinese科目的成绩,计算总分和平均分 chinese_score=[i_item['chinese'] for i_item in stus] print(chinese_score) print('chinese总分数为:'+str(sum(chinese_score))+";平均成绩为:"+str(sum(chinese_score)/len(chinese_score))) # 输出结果: # [81, 85, 82, 78, 88] # chinese总分数为:414;平均成绩为:82.8

2、根据指定key字段,筛选、删除指定value值的数据记录

stus = [{'name': '小王', 'age': 14, 'chinese': 81, 'math': 92, 'english': 80}, {'name': '小刘', 'age': 13, 'chinese': 85, 'math': 77, 'english': 83}, {'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小吴', 'age': 12, 'chinese': 78, 'math': 71, 'english': 74}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # 保留name为小张和小黄的数据,单个对象用等于号=即可 stus_t=[i for i in stus if i['name'] in ('小张','小黄')] print(stus_t) stus_t=[i for i in stus if i['name'] in ['小张','小黄']] print(stus_t) # 输出结果: # [{'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # [{'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # 删除name为小王和小吴的数据,单个对象用等于号=即可 stus_t=[i for i in stus if i['name'] not in ('小王','小吴')] print(stus_t) stus_t=[i for i in stus if not i['name'] in ('小王','小吴')] print(stus_t) # 输出结果: # [{'name': '小刘', 'age': 13, 'chinese': 85, 'math': 77, 'english': 83}, {'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # [{'name': '小刘', 'age': 13, 'chinese': 85, 'math': 77, 'english': 83}, {'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}]

3、那么,这里我们把1和2的方法结合起来,进行组合操作,就可以得到:根据指定key筛选数据,再对其他key进行统计操作(相当于sql中的 sum 操作和 where 操作的组合)

stus = [{'name': '小王', 'age': 14, 'chinese': 81, 'math': 92, 'english': 80}, {'name': '小刘', 'age': 13, 'chinese': 85, 'math': 77, 'english': 83}, {'name': '小张', 'age': 14, 'chinese': 82, 'math': 84, 'english': 79}, {'name': '小吴', 'age': 12, 'chinese': 78, 'math': 71, 'english': 74}, {'name': '小黄', 'age': 15, 'chinese': 88, 'math': 79, 'english': 87}] # 保留name为小王、小张、小黄的数据,,计算math总分和平均分 math_score=[i_item['math'] for i_item in [i for i in stus if i['name'] in ('小王','小张','小黄')]] print(math_score) print('小王+小张+小黄 的chinese总分数为:'+str(sum(math_score))+";平均成绩为:"+str(sum(math_score)/len(math_score))) # 输出结果: # [92, 84, 79] # 小王+小张+小黄 的chinese总分数为:255;平均成绩为:85.0



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3