python 字典格式嵌套,相同项做叠加

all_dict = {}


for tg_id in ['com.qq_a','com.qq_b','com.qq_c','com.qq_c']:
    tmp_dict = all_dict.get(tg_id,{'times':0})
    tmp_dict['times'] += 123
    all_dict[tg_id] = tmp_dict

print(all_dict)

#{'com.qq_a': {'times': 123}, 'com.qq_b': {'times': 123}, 'com.qq_c': {'times': 246}}

 

posted on 2018-04-02 21:22  星河赵  阅读(366)  评论(0编辑  收藏  举报

导航