在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  HTML/ python 數(shù)據(jù)合并提問

python 數(shù)據(jù)合并提問

[

     {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x},
     {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x},
     {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x}]
     

這是我的數(shù)據(jù)結(jié)構(gòu),其中 couse_id 有很多相同的,但 dosage 不同,我想快速把 相同的 souse_id的dosage合并后還保持這個數(shù)據(jù)結(jié)構(gòu),但沒想到高效的方法,所以來求助指點謝謝。

回答
編輯回答
赱丅呿

不知道是不是高效的方法。


def _merge_couse(r,item):
    id = item['couse_id']
    if id in r:        
        r[id]['dosage']=r[id]['dosage']+item['dosage'] # 可以寫成你需要的合并方式
    else:
        r[id]=item
    return r

def merge_data(data):
    return list(reduce(_merge_couse,data,{}).values())    
    
2017年4月16日 19:29