[Python] itertools module
accumulate(iterable[, func, *, initial=None]) This function makes an iterator that returns accumulated sums, or accumulated results of other binary functions. The default func is operator.add function. from itertools import accumulate import operator print(list(accumulate([1, 2, 3, 4]))) # [1, 3, 6, 10] print(list(accumulate([1, 2, 3, 4], operator.mul))) # [1, 2, 6, 24] print(list(accumulate([1,..
2021. 5. 15.
[Python] ChainMap in collections
ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. from collections import ChainMap dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} cm = ChainMap(dict1, dict2) print(cm) # ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) print(list(cm.keys())) # ['b', 'c', 'a'] print(list(cm.values())) # [2, 4, 1] print(list(cm.items())) # [('b', 2), ('c', ..
2021. 5. 2.