且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

合并嵌套列表而不影响python中的键和值方向

更新时间:2023-11-28 23:21:04

您可以执行以下操作:

def convert(a):
    d = {}
    for x in a:
        for key,val in x.items():
            if key not in d:
                d[key] = []
            d[key] += val
    return d

上面的代码适用于Python 3.

Code above is for Python 3.

如果您使用的是python 2.7,那么我认为您应该将items替换为iteritems.

If you're on Python 2.7, then I believe that you should replace items with iteritems.