Python字典-合并字典

时间:2020-03-16 19:55:45

标签: python-3.x

给出字典列表,返回所有键组合在一起的新字典。

这是我到目前为止所做的:

def combine_dictionaries(dictionary_list):
    # your code goes here
    my_dictionary = {}

    for key in dictionary_list:

        my_dictionary.update(key, dictionary_list[key])

    return my_dictionary

这是它产生的错误: list indices must be integers or slices, not dict

有人告诉我,如何给我提供字典列表,如何获得整数?

预期结果应如下所示:

{'a':3,'b':2,'c':4,4:4,3:3}

1 个答案:

答案 0 :(得分:0)

您的功能差不多了。
我相信您应该只在字典更新中传递key,因为update built-in function会接受另一个字典对象或键/值对的迭代。

def combine_dictionaries(dictionary_list):
    my_dictionary = {}
    for key in dictionary_list:
        my_dictionary.update(key)
    return my_dictionary