【问题标题】:Python - Update dictionary int values by adding from another dictionary Counter()Python - 通过从另一个字典 Counter() 添加来更新字典 int 值
【发布时间】:2016-02-14 00:44:11
【问题描述】:
origincount = {
        'Bug': 0,
        'Important': 0,
        'User Story': 0,
        'Critical': 0,
        'Moderate': 0,
        'Low': 0,
        'story': 0
    }
    if epic['archived'] is False:
        for story in stories:
            if story['epic'] is not None and story['epic']['id'] == epic['id']:
                counts = Counter(label['name'] for label in story['labels'])
                origincount.update(counts)

当值需要一起增加而不仅仅是“更新”时,我无法理解字典更新。

从上面的示例中,当 origincount.update(counts) 发生时,它只是将值更改为计数中的任何值。在下一个外观它做同样的事情。

问题是我需要通过计数来增加值,而不仅仅是替换它。

我尝试遍历计数并针对它不为 0 执行 if,然后添加执行 origincount['Bug'] = counts['Bug'] + origincount['Bug']。但由于我自己的错误,这似乎失败了,或者根本没有。

更新:

for epic in epics:
    origin = Counter({
        'Bug': 0,
        'Important': 0,
        'User Story': 0,
        'Critical': 0,
        'Moderate': 0,
        'Low': 0,
        'story': 0
    })

    if epic['archived'] is False:
        for story in stories:
            if story['epic'] is not None and story['epic']['id'] == epic['id']:
                # story_total += 1
                counts = Counter(label['name'] for label in story['labels'])
                print(counts)
                origin = origin + counts

print(origin)

输出:

Counter({'Bug': 1, 'Important': 1})
Counter({'Critical': 1, 'Bug': 1})
Counter({'User Story': 1})
Counter({'Bug': 1})
Counter({'Bug': 1, 'Important': 1})
Counter({'Bug': 1, 'Important': 1})
Counter({'Critical': 1, 'Bug': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'Critical': 1, 'Bug': 1})
Counter({'Critical': 1, 'Bug': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'User Story': 1})
Counter({'Critical': 1, 'Bug': 1})
Counter({'Critical': 0, 'User Story': 0, 'Important': 0, 'story': 0, 'Low': 0, 'Bug': 0, 'Moderate': 0})

即使对原始字典进行了计数器转换,它仍然不会将它们相加。

【问题讨论】:

  • 你可以一起添加计数器
  • 所以 origincount = origincount + counts ?类型错误:+ 的不支持的操作数类型:'dict' 和 'Counter' origincount 不是一个计数器,只是一个 dict
  • 问题是origincount 不是Counter。你为什么不做一个?
  • 是的。 totalcount = Counter(origin),然后做了一个 totalcount.update(counts)。由于某种原因,它仍然没有更新。
  • @timgeb 原帖已更新。包括输出。仍然没有变化。

标签: python dictionary counter


【解决方案1】:

我认为这会起作用,迭代 origincount,为每个检查它是否在您的 othercount 中,如果是则更新 origincount 值。

for key, value in origincount.items():
    if key in othercount.keys():
        origincount[key] = value + othercount[key]

【讨论】:

  • 我正在转向 counter() 选项,但由于某种原因它仍然不会将值相加。
  • 对不起,我之前没有使用过 python Counter 类。我的解决方案是结合来自 2 个单独字典的计数。
【解决方案2】:

在循环的每次迭代中,您似乎都在声明和初始化一个新的 Counter 对象。

我愿意打赌它会结束循环,然后 origincounts 在最后一次迭代中被清零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2022-07-10
    • 2023-03-11
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多