如何使字典的一个元素在 python 中累积其他元素

How to make an element of a dict accumulate other elements in python

我有以下 python 字典列表的结构(我们称之为 dict1):

dict1 = {"word1": {'111.txt': 1, '112.txt': 3, '113.txt': 2},
         "word2": {'111.txt': 2, '112.txt': 2, '113.txt': 1},
         "word3": {'111.txt': 1, '113.txt': 1},
         "word4": {'111.txt': 3, '113.txt': 2},
         "word5": {'111.txt': 5, '113.txt': 1}}

我想创建一个 新字典 (dict2),其中我有 dict1 的键和 dict1 的元素之和该键作为其元素。因此:

{'111.txt': 12, '112.txt': 5, '113.txt': 7}

我尝试在下面做下面的代码,但是,它只是将dict1的最后一个元素存储在dict2中,也就是说,它没有累加[=的值15=]

for i,j in dict1.items():
     for k,w in j.items():
            dict2[k] =+ j[k]

输出结果如下,只留下dict1的最后一个元素,不累加和

{'111.txt': 5, '112.txt': 2, '113.txt': 1}

有人知道代码中可能有什么问题吗?或者你有更好的主意吗?

我认为错误出在您初始化 dict2 的方式上,但由于您尚未发布该部分,所以无法说明太多。不过这应该有效:

dict1 = {"word1": {'111.txt': 1, '112.txt': 3, '113.txt': 2},
         "word2": {'111.txt': 2, '112.txt': 2, '113.txt': 1},
         "word3": {'111.txt': 1, '113.txt': 1},
         "word4": {'111.txt': 3, '113.txt': 2},
         "word5": {'111.txt': 5, '113.txt': 1}}

dict2 = dict()

for i, j in dict1.items():
    for k, w in j.items():
        dict2[k] = dict2.get(k, 0) + j[k]

print(dict2)

输出:

{'112.txt': 5, '113.txt': 7, '111.txt': 12}

我不确定你是如何初始化的dict2,所以很难指出你代码中的问题。话虽如此,以下是解决此问题的一些方法。

假设您的数据采用嵌套字典格式 {'word1': {'111.txt': 1, '112.txt': 3, '113.txt': 2}, 'word2':..},我相信您的目标是:

d = {
    'word1': {'111.txt': 1, '112.txt': 3, '113.txt': 2},
    'word2': {'111.txt': 2, '112.txt': 2, '113.txt': 1},
    'word3': {'111.txt': 1, '113.txt': 1},
    'word4': {'111.txt': 3, '113.txt': 2},
    'word5': {'111.txt': 5, '113.txt': 1}
}

counts = {}

# only need to iterate values here. 'word1', 'word2' etc. not needed in output
for v1 in d.values():

    # iterate sub dictionary values and keys. These are needed for output. 
    for k, v2 in v1.items():

        # Use dict.get() to set initial value to 0 if key doesn't exist
        counts[k] = v2 + counts.get(k, 0)

print(counts)
# {'111.txt': 12, '112.txt': 5, '113.txt': 7}

或者像下面这样的简单方法:

counts = {}
for v1 in d.values():
    for k, v2 in v1.items():

        # initialize to 0 if key doesn't exist
        if k not in counts:
            counts[k] = 0

        # Continue counting, since above condition will prevent KeyError
        counts[k] += v2

print(counts)
# {'111.txt': 12, '112.txt': 5, '113.txt': 7}

此外,您还可以在此处使用 collections.Counter

from collections import Counter

d = {
    'word1': {'111.txt': 1, '112.txt': 3, '113.txt': 2},
    'word2': {'111.txt': 2, '112.txt': 2, '113.txt': 1},
    'word3': {'111.txt': 1, '113.txt': 1},
    'word4': {'111.txt': 3, '113.txt': 2},
    'word5': {'111.txt': 5, '113.txt': 1}
}

counts = Counter()
for v in d.values():
    counts.update(v)

print(counts)
# Counter({'111.txt': 12, '113.txt': 7, '112.txt': 5})

使用 Counter.update() 轻松添加计数。

你也可以在这里使用collections.defaultdict(int):

from collections import defaultdict

d = {
    'word1': {'111.txt': 1, '112.txt': 3, '113.txt': 2},
    'word2': {'111.txt': 2, '112.txt': 2, '113.txt': 1},
    'word3': {'111.txt': 1, '113.txt': 1},
    'word4': {'111.txt': 3, '113.txt': 2},
    'word5': {'111.txt': 5, '113.txt': 1}
}

counts = defaultdict(int)
for v1 in d.values():
    for k, v2 in v1.items():
        counts[k] += v2

print(counts)
# defaultdict(<class 'int'>, {'111.txt': 12, '112.txt': 5, '113.txt': 7})

注意: Counterdefaultdict 都是 dict 的子类,因此您可以像普通词典一样对待它们。如果您真的希望输出为 dict,则可以强制转换 dict():

print(dict(counts))
# {'111.txt': 12, '112.txt': 5, '113.txt': 7}

它们也都为您处理初始化,因此您无需使用 0 初始化新密钥。

您在这里遇到的问题是您需要使用 += 而不是 =+。通过这个 code example in python tutor and you'll see that =+ is treated as an assignment not an in-place addition. Here 是带有 += 的示例代码和一些额外的逻辑,您会正确地看到它。