嵌套字典的逻辑实现--hu

-- coding:utf-8 --

charset = utf-8

dict = {}

dict = {“time1”: {“author”:[“stat”,]}, “time2”: {“author”:[“stat”,]}}

stat = dict[“time1”].get(“author”)

print stat

dictlist = {“dmk1”:{“time1”:{“author1”:[1,2,4,2], “author2”:[1,2,4,2], “author3”:[1,2,4,2], “author4”:[1,2,4,2]},“time2”:{“author1”:[1,2,4,2], “author2”:[1,2,4,2], “author3”:[1,2,4,2]}},
“dmk2”:{“time1”:{“author1”:[1,1,1,1], “author2”:[1,2,4,2], “author3”:[1,2,4,2]},“time2”:{“author1”:[1,2,4,2], “author2”:[1,2,4,2], “author3”:[1,2,4,2]}},
“dmk3”:{“time1”:{},“time2”:{“author1”:[1,109,4,2], “author2”:[1,3079,4,2], “author3”:[1,2,4,2]}}}

def dict_sum():
for dmk in dictlist.items():
print “dmk”, dmk
timelist = dmk[1]
print “time”,timelist
for time in timelist.items():
authorlist = time[1]
print “authorlist”, authorlist
if dict.get(time[0]):
if authorlist is None:
continue
for author, stat in authorlist.items():
print “author”,author, “stat”,stat
# 如果在dict的这个时间中有这个用户则加用户数据,没有就加入用户列表
if dict[time[0]].get(author) is None:
authorlist[author] = stat
else:
dict_stat = dict[time[0]][author]
dict_stat[0] += stat[0]
dict_stat[1] += stat[1]
dict_stat[2] += stat[2]
dict_stat[3] += stat[3]
else:
dict[time[0]] = authorlist

    #     print "dict1", dict
    # print "dict2", dict
return dict

dic = dict_sum()
print dic

{‘time1’: {‘author4’: [1, 2, 4, 2], ‘author1’: [2, 3, 5, 3], ‘author2’: [2, 4, 8, 4], ‘author3’: [2, 4, 8, 4]}, ‘time2’: {‘author1’: [3, 113, 12, 6], ‘author2’: [3, 3083, 12, 6], ‘author3’: [3, 6, 12, 6]}}

{‘time1’: {‘author4’: [1, 2, 4, 2], ‘author1’: [2, 3, 5, 3], ‘author2’: [2, 4, 8, 4], ‘author3’: [2, 4, 8, 4]}, ‘time2’: {‘author1’: [3, 113, 12, 6], ‘author2’: [3, 3083, 12, 6], ‘author3’: [3, 6, 12, 6]}}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值