基于单独python列表中键的总和值

发布于 2025-02-08 11:03:55 字数 337 浏览 1 评论 0原文

我有一个名为hits的字典。

hits = {"The Weeknd": 6, "Maroon 5": 0, "Justin Bieber": 8, "Post Malone": 5}

我还有一个单独的独立列表,标题为Artists

artists = ["The Weeknd", "Justin Bieber"]

我想根据Artists中列出的键 hits 字典中的总值总和。 /code>列表(即,答案为6 + 8 = 14)。

I have a dictionary called hits below.

hits = {"The Weeknd": 6, "Maroon 5": 0, "Justin Bieber": 8, "Post Malone": 5}

I also have a separate, standalone list titled artists:

artists = ["The Weeknd", "Justin Bieber"]

I'd like to sum the total values in the hits dictionary based on the keys listed in the artists list (i.e., answer would be 6 + 8 = 14).

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

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

发布评论

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

评论(1

遗弃M 2025-02-15 11:03:55
sum([v for k,v in hits.items() if k in artists ])

sum([v for k,v in hits.items() if k in artists ])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文