将 2 个字典列表合并到一个字典列表中
Combine 2 lists of dict in one list of dict
在 python3 我有 2 个列表:
listA = [{'aString': 'someone', 'aNumber': 123}]
listB = [{'anotherNumber': 456}]
如何将它们组合成 1 个如下所示的列表?
listC = [{'aString': 'someone', 'aNumber': 123, 'anotherNumber': 456}]
如果我用,
listC = listA + listB
我得到:
listC = [{'aString': 'someone', 'aNumber': 123}, {'anotherNumber': 456}]
您可以将两个列表解包到 dict 构造函数中,然后将两个 dict 解包为一个:
[{**dict(*listA), **dict(*listB)}]
尝试使用:
listC = dict(listA .items() + listB.items())
在 python3 我有 2 个列表:
listA = [{'aString': 'someone', 'aNumber': 123}]
listB = [{'anotherNumber': 456}]
如何将它们组合成 1 个如下所示的列表?
listC = [{'aString': 'someone', 'aNumber': 123, 'anotherNumber': 456}]
如果我用,
listC = listA + listB
我得到:
listC = [{'aString': 'someone', 'aNumber': 123}, {'anotherNumber': 456}]
您可以将两个列表解包到 dict 构造函数中,然后将两个 dict 解包为一个:
[{**dict(*listA), **dict(*listB)}]
尝试使用:
listC = dict(listA .items() + listB.items())