[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.
[Python] Underscore Usage
I'll post about the underscore(_) in Python. Basic Usage Previous Value If you use underscore in REPL, it points to the previous result. >>> 3 + 5 8 >>> _ 8 >>> _ + 7 15 Ignoring Value You can use underscore for ignoring values. a, _, b = (1, 2, 3) print(f"{a}, {b}") # 1, 3 a, *_, b = (1, 2, 3, 4, 5, 6, 7) print(f"{a}, {b}") # 1, 7 If you use underscore in a loop, it indicates the current value ..
2021. 4. 26.