A pythonic way to sum the values of a dict with a specified key list?

xuhdev

Let's say I have a list (or other iterable object) ['a','b','c','d']. I have a dict X. I can sum them up in the naive way:

s = 0
for k in ['a','b','c','d']:
    s += X[k]

But is there a more pythonic way?

Reut Sharabani

You can do:

s = sum(X[k] for k in ['a','b','c','d'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Pythonic and compact way to compare the key in dict

来自分类Dev

What's a fast and pythonic/clean way of removing a sorted list from another sorted list in python?

来自分类Dev

如何将`key:list`对的dict转换为元组列表?

来自分类Dev

What is the fastest way to create a list of directories specified in a file?

来自分类Dev

用函数总和更新dict的pythonic方法

来自分类Dev

Excel: Sum a range of cells if a related key is in a range of values using a formula

来自分类Dev

What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and replace with values)

来自分类Dev

python sum dict键和sum dict值

来自分类Dev

Pythonic Way to compare two unordered lists by attributes

来自分类Dev

Most pythonic way to convert a string to a octal number

来自分类Dev

Pythonic way to sparsely randomly populate array?

来自分类Dev

Find nearest neighbour in a more pythonic way

来自分类Dev

Pythonic和紧凑的方法来比较dict中的键

来自分类Dev

Pythonic和紧凑的方法来比较dict中的键

来自分类Dev

Getting a list of unique hash key values from dynamodb using boto

来自分类Dev

将List <Keys>和List <Values>合并到List <Hashmap <Key,Value >>

来自分类Dev

Dict and List Manipulation Python

来自分类Dev

scala sum By Key功能

来自分类Dev

What's the Pythonic way to report nonfatal errors in a parser?

来自分类Dev

键入dict.keys(),但dict.get(key)返回None

来自分类Dev

sum(list)函数Python

来自分类Dev

有没有一种处理树形结构的dict键的pythonic方法?

来自分类Dev

从两个不同列表中的 dict 中删除重复键/值的更多 Pythonic 方法

来自分类Dev

Sort An array with its specified key value PHP

来自分类Dev

从dict和key的存在中得出对或错

来自分类Dev

python json dict迭代{key:value}是相同的

来自分类Dev

python random dict key,并访问它

来自分类Dev

复制 dict 遗漏一对 (key, val)

来自分类Dev

以pythonic的方式获取list的每个值的索引列表

Related 相关文章

  1. 1

    Pythonic and compact way to compare the key in dict

  2. 2

    What's a fast and pythonic/clean way of removing a sorted list from another sorted list in python?

  3. 3

    如何将`key:list`对的dict转换为元组列表?

  4. 4

    What is the fastest way to create a list of directories specified in a file?

  5. 5

    用函数总和更新dict的pythonic方法

  6. 6

    Excel: Sum a range of cells if a related key is in a range of values using a formula

  7. 7

    What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and replace with values)

  8. 8

    python sum dict键和sum dict值

  9. 9

    Pythonic Way to compare two unordered lists by attributes

  10. 10

    Most pythonic way to convert a string to a octal number

  11. 11

    Pythonic way to sparsely randomly populate array?

  12. 12

    Find nearest neighbour in a more pythonic way

  13. 13

    Pythonic和紧凑的方法来比较dict中的键

  14. 14

    Pythonic和紧凑的方法来比较dict中的键

  15. 15

    Getting a list of unique hash key values from dynamodb using boto

  16. 16

    将List <Keys>和List <Values>合并到List <Hashmap <Key,Value >>

  17. 17

    Dict and List Manipulation Python

  18. 18

    scala sum By Key功能

  19. 19

    What's the Pythonic way to report nonfatal errors in a parser?

  20. 20

    键入dict.keys(),但dict.get(key)返回None

  21. 21

    sum(list)函数Python

  22. 22

    有没有一种处理树形结构的dict键的pythonic方法?

  23. 23

    从两个不同列表中的 dict 中删除重复键/值的更多 Pythonic 方法

  24. 24

    Sort An array with its specified key value PHP

  25. 25

    从dict和key的存在中得出对或错

  26. 26

    python json dict迭代{key:value}是相同的

  27. 27

    python random dict key,并访问它

  28. 28

    复制 dict 遗漏一对 (key, val)

  29. 29

    以pythonic的方式获取list的每个值的索引列表

热门标签

归档