在Python中使用Counts来减去字典列表

发布于 2025-01-19 17:41:09 字数 895 浏览 0 评论 0原文

我已经计算出如何使用“计数器”将字典列表与下面的代码添加列表

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a + b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

,但是,当我尝试减去错误时,我会遇到错误。例如:

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a - b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

有人知道这项工作吗?还是我如何解决这个问题?

我尝试在计数器中使用减法功能,但似乎仍然不起作用

I've worked out how to use "counter" to add lists of dictionarys with the code below

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a + b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

However, when I try to subtract I get an error. For example:

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a - b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

Is anyone aware of a work around to this? Or how I could approach this issue?

I've tried using the subtract function in counter but it still doesn't seem to work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2025-01-26 17:41:09

我认为计数器不接受否定计数,但是如果您不介意没有负数,这将起作用(仅分别进行两个计数器):

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a), Counter())
joint -= sum((Counter({elem['num']: elem['count']}) for elem in b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

它输出:

[{'num': 'star2', 'count': 1}]

其他值(Star1和Star3)都会少少比0分别达到1-7 = -6 = -6 = -1

,您可以不使用计数器并这样做(这支持负数和您想要的任何操作):

a_info = {d['num']:d['count'] for d in a}
b_info = {d['num']:d['count'] for d in b}
[{'num':item, 'count': a_info.get(item, 0)-b_info.get(item, 0)} for item in set(a_info)|set(b_info)]

I don't think Counter accept negative counts, but if you don't mind about not having negative counts, this will work (just doing the two Counters separately):

from collections import Counter
a = [{'num': 'star1', 'count': 1},
     {'num': 'star2', 'count': 3}]
b = [{'num': 'star1', 'count': 7},
      {'num': 'star2', 'count': 2},
      {'num': 'star3', 'count': 1}]
joint = sum((Counter({elem['num']: elem['count']}) for elem in a), Counter())
joint -= sum((Counter({elem['num']: elem['count']}) for elem in b), Counter())
[{'num': num, 'count': counts} for num, counts in joint .items()]

It outputs:

[{'num': 'star2', 'count': 1}]

The other values (star1 and star3) would both be less than 0 because they come to 1-7=-6 and 0-1=-1 respectively

Alternatively you can just not use counters and do it like this (this supports negative numbers and whatever operation you want):

a_info = {d['num']:d['count'] for d in a}
b_info = {d['num']:d['count'] for d in b}
[{'num':item, 'count': a_info.get(item, 0)-b_info.get(item, 0)} for item in set(a_info)|set(b_info)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文