简体   繁体   English

如何更新 Python 字典中的元素?

[英]How to update elements in a dictionary in Python?

I have a list of dictionary in which there are other lists of object我有一个字典列表,其中还有 object 的其他列表

{'Kitchen_Activity': [date= 2009-10-16, start time= 08:45:38, end time= 08:58:52, duration= 0 days 00:13:14 ]}
{'Chores': [date= 2009-10-16, start time= 08:59:02, end time= 09:14:47, duration= 0 days 00:15:45 ]}
{'Kitchen_Activity': [date= 2009-10-16, start time= 09:14:40, end time= 09:14:54, duration= 0 days 00:00:14 ]}
{'Chores': [date= 2009-10-16, start time= 09:30:40, end time= 09:58:54, duration= 0 days 00:28:14 ]}
{'Kitchen_Activity': [date= 2009-10-16, start time= 10:14:40, end time= 10:14:54, duration= 0 days 00:00:14 ]}
{'Shower': [date= 2009-10-16, start time= 11:14:40, end time= 11:40:40, duration= 0 days 00:26:00 ]}

and I want to achive this kind of list:我想获得这种列表:

{'Kitchen_Activity': [date= 2009-10-16, start time= 08:45:38, end time= 08:58:52, duration= 0 days 00:13:14 ], [date= 2009-10-16, start time= 09:14:40, end time= 09:14:54, duration= 0 days 00:00:14 ], [date= 2009-10-16, start time= 10:14:40, end time= 10:14:54, duration= 0 days 00:00:14 ]}
{'Chores': [date= 2009-10-16, start time= 08:59:02, end time= 09:14:47, duration= 0 days 00:15:45 ], [date= 2009-10-16, start time= 09:30:40, end time= 09:58:54, duration= 0 days 00:28:14 ]}
{'Shower': [date= 2009-10-16, start time= 11:14:40, end time= 11:40:40, duration= 0 days 00:26:00 ]}

What I've tried doing is this我试过的是这个

def generateActivity(self, date, name, ts, te, dur):
        activities = {}
        activity = Activity().generateInstance(date, name, ts, te, dur)

        if activity.name not in activities.keys():
            activities[activity.name] = []
            activities[activity.name].append(activity)
        else:
            for key, item in activities.items():
                if key == activity.name:
                    item.append(activity)
        return activities

The list I put up there is the output of this function that I call in the main class. What's wrong with what I'm doing?我放在那里的列表是这个 function 的 output,我在主 class 中调用它。我在做什么有什么问题?

The list which you want to recieve follows the order, which isn't allowed for key values in a dictionary您要接收的列表遵循字典中的键值不允许的顺序

{
    a:[],[]
    b:[],[]
}

Instead you should try for相反,你应该尝试

{
    a:[[],[]]
    b:[[],[]]
}

I take your list of dictionary as dicts我把你的字典列表当作字典

If you want the above result, you make a dictionary value string (used json.dumps())如果你想要上面的结果,你制作一个字典值字符串(使用json.dumps())

import json
update_dict = {}
for dict in dicts:
    current_key = list(dict.keys())[0]
    if not current_key in update_dict:
        update_dict[current_key] = json.dumps(dict[current_key])
    else:
        update_dict[current_key] = update_dict[current_key] +", "+json.dumps(dict[current_key])

Otherwise you get the value list of list value.否则,您将获得列表值的值列表。

update_dict = {}
for dict in dicts:
    current_key = list(dict.keys())[0]
    if not current_key in update_dict:
       update_dict.update({current_key:[]})
       update_dict[current_key].append(dict[current_key])
    else:
        update_dict[current_key].append(dict[current_key])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM