在嵌套字典中将列表元素相加 - Python

2024-04-25 15:06:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有嵌套的字典,下面是我到目前为止的代码的输出,这正是我所需要和想要的。我尝试将第二个ID号字典组的list元素添加到一起,以获得每个元素相对于第一个字典发生的总次数,这是一个特定的日期

{'2014-06-14': {'205089113': [0, 1, 0, False, True], '268243219': [0, 1, 0, False, True], '216535211': [0, 0, 0, False, True], '207385741': [0, 0, 0, False, True], '207018490': [0, 0, 0, False, True], '204059430': [0, 0, 0, False, True], '209169283': [0, 1, 0, False, True], '232067397': [0, 0, 0, False, True], '204718647': [0, 1, 0, False, True], '205438195': [0, 1, 0, False, True]}, '2014-06-20': {'209989276': [1, 0, 0, False, True], '209840693': [1, 0, 0, False, True], '207067397': [1, 0, 0, False, True], '207919002': [1, 0, 0, False, True], '204718498': [2, 1, 0, False, True], '204437024': [1, 0, 0, False, True], '219878931': [1, 0, 0, False, True]},

我的意思的一个例子是 '2014-06-14':[0,5,0,0,10],'2014-06-20': [8,1,0,0,7]}

每次尝试len()甚至Counter模块时,都会出现一个键错误

下面的代码给出了第二个字典的出现次数,但似乎无法破解将其列表元素添加在一起的代码,不确定是否有我不理解的直接方法,或者是否可能

for date in dates.keys(): print date, len(dates[date].keys())

试图指出正确的方向


Tags: 模块代码idfalsetrue元素datelen
2条回答

我假设您发布的输出是错误的,并且预期的输出等于{'2014-06-14':[0,5,0,0,10],'2014-06-20': [8,1,0,0,7]}。以下将满足您的需要:

d = {'2014-06-14': {'205089113': [0, 1, 0, False, True],
                    '268243219': [0, 1, 0, False, True],
                    '216535211': [0, 0, 0, False, True],
                    '207385741': [0, 0, 0, False, True],
                    '207018490': [0, 0, 0, False, True],
                    '204059430': [0, 0, 0, False, True],
                    '209169283': [0, 1, 0, False, True],
                    '232067397': [0, 0, 0, False, True],
                    '204718647': [0, 1, 0, False, True],
                    '205438195': [0, 1, 0, False, True]},
     '2014-06-20': {'209989276': [1, 0, 0, False, True],
                    '209840693': [1, 0, 0, False, True],
                    '207067397': [1, 0, 0, False, True],
                    '207919002': [1, 0, 0, False, True],
                    '204718498': [2, 1, 0, False, True],
                    '204437024': [1, 0, 0, False, True],
                    '219878931': [1, 0, 0, False, True]}}

result = {}
for key in d:
    values = zip(*d[key].values())
    result[key] = [sum(value_tuple) for value_tuple in values]
print result

如果您将子字典转换为一个数据帧,它将允许您轻松地完成:

import pandas as pd

d = {'2014-06-14': {'205089113': [0, 1, 0, False, True], 
                    '268243219': [0, 1, 0, False, True], 
                    '216535211': [0, 0, 0, False, True], 
                    '207385741': [0, 0, 0, False, True], 
                    '207018490': [0, 0, 0, False, True], 
                    '204059430': [0, 0, 0, False, True], 
                    '209169283': [0, 1, 0, False, True], 
                    '232067397': [0, 0, 0, False, True], 
                    '204718647': [0, 1, 0, False, True], 
                    '205438195': [0, 1, 0, False, True]}, 
     '2014-06-20': {'209989276': [1, 0, 0, False, True], 
                    '209840693': [1, 0, 0, False, True], 
                    '207067397': [1, 0, 0, False, True], 
                    '207919002': [1, 0, 0, False, True], 
                    '204718498': [2, 1, 0, False, True], 
                    '204437024': [1, 0, 0, False, True], 
                    '219878931': [1, 0, 0, False, True]}}

new_d = {}
for k in d.iterkeys():
    new_d[k] = pd.DataFrame.from_dict(d[k], orient="index")

然后:

out = {}
for k in new_d:
    out[k] = new_d[k].sum().tolist()

print out
{'2014-06-14': [0.0, 5.0, 0.0, 0.0, 10.0], '2014-06-20': [8.0, 1.0, 0.0, 0.0, 7.0]}

拥有熊猫数据帧将允许您轻松地应用其他功能。你知道吗

希望这有帮助。你知道吗

相关问题 更多 >