简体   繁体   English

如何在字典中添加单个键的值

[英]How to add values of a single key in a dictionary

Say I have a dictionary:假设我有一本字典:

a_dictionary = {"a": [1, 2], "b": [3, 4]}

I want to get a resultant dictionary that has the value:我想得到一个具有以下值的结果字典:

a_dictionary = {"a": [3], "b": [7]}

Can someone please help me?有人可以帮帮我吗?

Try this:尝试这个:

for k,v in a_dictionary.items():
    a_dictionary[k] = [sum(v)]

Output:输出:

>>> print(a_dictionary)
{'a': [3], 'b': [7]}

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

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