본문 바로가기

전체 글346

[Python] functools module The functools module is for higher-order functions. @functools.cache(func) Simple lightweight unbounded function cache. Sometimes called memoize. Returns the same as lru_cache(maxsize=None). This function is smaller and faster than lru_cache() with a size limit because it never needs to evict old values. v3.9 or later from functools import cache @cache def factorial(n): return n * factorial(n - .. 2021. 5. 8.
[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] namedtuple in collections namedtuple can give a name to each element of tuple. from collections import namedtuple Point = namedtuple('Name', ['x', 'y']) pt = Point(1, 2) print(pt) # Name(x=1, y=2) print(pt.x, pt.y) # 1 2 Attributes _fields Tuple of strings listing the field names. print(pt._fields) # ('x', 'y') _field_defaults Dictionary mapping field names to default values. Point = namedtuple('Name', ['x', 'y'], defaul.. 2021. 5. 2.
[Python] Counter in collections Counter is a subclass of dict for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. from collections import Counter d = Counter("I have one dog and one cat".split()) print(d) # Counter({'one': 2, 'I': 1, 'have': 1, 'dog': 1, 'and.. 2021. 5. 1.
[Python] OrderedDict in collections OrderedDict is like dict but have some extra capabilities relating to ordering operation. OrderedDict is designed to be good at reordering operations. Space efficiency, iteration speed, and the performance of update operations were secondary. from collections import OrderedDict d = OrderedDict() d['apple'] = 1 d['banana'] = 2 d['cherry'] = 3 for k, v in d.items(): print(k, v) # apple 1 # banana .. 2021. 5. 1.
[Python] defaultdict in collections defaultdict is a subclass of the built-in dict class. dict returns KeyError if you get value with nonexistence key. In contrast, defaultdict returns default value even with the nonexistence key. Usage Default Value with Built-in Class from collections import defaultdict d = defaultdict(int) d['apple'] = 3 print(d['banana']) # 0 print(d) # defaultdict(, {'apple': 3, 'banana': 0}) Default Value wi.. 2021. 5. 1.
[Python] deque in collections Deque is a Double-Ended Queue. Deque supports thread-safe, memory-efficient appends and pops from either side. Import from collections import deque Attributes maxlen Maximum size of a deque or None if unbounded. Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end. d = deque([1, 2, 3], 3) print(d) # deque([1, 2, 3], ma.. 2021. 4. 30.
[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.
[Mac] 외장하드 포맷하기 > Big Sur 11.2.3 최근 외장하드를 구입해서 맥에서 사용을 하기 위해 초기화 작업을 해봤습니다. 먼저 LaunchPad를 실행시켜주세요. 맥용 키보드를 사용하고 계시면, F4 키를 누르면 됩니다. 그리고, Disk Utility를 찾아서 실행시켜줍니다. 못찾으시면, Other 폴더를 살펴봐주세요. Spotlight에서 찾으셔도 되고, 저같은 경우에는 Alfred를 사용해서 찾았어요. 그럼 이런 화면을 보실 수 있습니다. 꽂자마자 바로 ExFAT로 잡혀 있어서 즉시 사용할 수 있어요. 이런 편리함 때문에 맥을 쓰는 거죠. 하지만, ExFAT의 경우 윈도우와 호환이 되서 별도의 프로그램의 도움 없이 양쪽에서 사용할 수 있지만, 파일 시스템 자체의 안정성이 떨어져서 데이터 손실의 위험이 있습니다. .. 2021. 4. 21.