python合并list中各个字典中相同条件的累计数
Posted 菜鸟到大神的蜕变
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python合并list中各个字典中相同条件的累计数相关的知识,希望对你有一定的参考价值。
#coding: utf-8
import json
def test():
aa = [{u‘iTestID‘: 92478, u‘all_case_count‘: 45103, u‘refresh_time‘: u‘2016-11-29 17:03:01‘, u‘version_num‘: u‘BaseOn_W16.38_PassList_0916‘, u‘test_month‘: u‘09‘, u‘bug_count‘: 347, u‘fail_count‘: 420,u‘test_week‘: u‘37‘, u‘summary_type‘: u‘TRUNK‘, u‘confirm_rate‘: u‘100.0000%‘, u‘pass_count‘: 44683, u‘test_year‘: u‘2016‘, u‘id‘: 716}, {u‘iTestID‘: 92477, u‘all_case_count‘: 953, u‘refresh_time‘: u‘2016-11-29 17:03:01‘, u‘version_num‘: u‘FM_BASE_W16.38‘, u‘test_month‘: u‘09‘, u‘bug_count‘: 188, u‘fail_count‘: 301, u‘test_week‘: u‘37‘, u‘summary_type‘: u‘TRUNK‘, u‘confirm_rate‘: u‘100.0000%‘, u‘pass_count‘: 652, u‘test_year‘: u‘2016‘, u‘id‘: 717}]
result = []
for i in range(0,len(aa)):
sum_all_case_count,sum_pass_count,sum_bug_count, sum_fail_count,flag,sum_oldfail_count,sum_newfail_count = 0,0,0,0,0,0,0
for r in result:
bb = aa[i][‘version_num‘].split(‘_W‘)
if bb[1][:5] in r[‘version_num‘]:
flag = 1
if flag == 0:
for j in range(0,len(aa)):
print aa[j][‘version_num‘].split(‘_W‘)
if (aa[i][‘version_num‘].split(‘_W‘))[1][:5] in (aa[j][‘version_num‘].split(‘_W‘)[1]):
#aa[i][‘version_num‘] = (aa[i][‘version_num‘].split(‘_W‘))[1][0:5]
sum_all_case_count = sum_all_case_count+aa[j][‘all_case_count‘]
sum_pass_count = sum_pass_count+aa[j][‘pass_count‘]
sum_bug_count = sum_bug_count+aa[j][‘bug_count‘]
sum_fail_count = sum_fail_count+aa[j][‘fail_count‘]
if ‘FM_BASE‘ in aa[j][‘version_num‘]:
sum_oldfail_count = sum_oldfail_count +aa[j][‘fail_count‘]
if ‘PassList‘ in aa[j][‘version_num‘]:
sum_newfail_count = sum_newfail_count +aa[j][‘fail_count‘]
one_record = dict(version_num=aa[i][‘version_num‘],
sum_all_case_count=sum_all_case_count,
sum_pass_count=sum_pass_count,
sum_bug_count=sum_bug_count,
sum_fail_count=sum_fail_count,
sum_oldfail_count=sum_oldfail_count,
sum_newfail_count =sum_newfail_count
)
result.append(one_record)
print result
if __name__ == ‘__main__‘:
test()
以上是关于python合并list中各个字典中相同条件的累计数的主要内容,如果未能解决你的问题,请参考以下文章
python 语言,list中合并重复字典,将value相加
Python 两个字典(dic)中相同key合并(value分别作合成后字典key和value)