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}}