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

当我们一次性获得众多数据时,相当于字典数组,如所有的学生科目成绩时,有时候会有需要对指定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

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值