ZIP (aggregate two iterators as list of tuples)
numbers = [1, 2, 3] letters = ["A", "B", "C"] print(list(zip(numbers,letters))) # list of tuples for pair in zip(numbers, letters): print(pair) OUTPUT: [(1, 'A'), (2, 'B'), (3, 'C')] (1, 'A') (2, 'B') (3, 'C')
ITERTOOLS
import itertools arr = ['A','B','C','D'] comb_arr = list(itertools.combinations(arr,2)) # 중복 X --> ORDER MATTERS (ex. only [A,B]) perm_arr = list(itertools.permutations(arr,2)) # 중복 X --> ORDER DOES NOT MATTER (ex. [A,B] and [B,A]) product_arr = list(itertools.product(arr,repeat = 2)) # 중복 O --> CARTESIAN PRODUCT comb_repl_arr = list(itertools.combinations_with_replacement(arr,2)) # 중복 O but [A,..
DICTIONARY
1. A~Z의 딕셔너리 빨리 만들기 dic = dict(zip(string.ascii_lowercase,[0]*26)) 2. 딕셔너리 키 기준 합치기 s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> sorted(d.items()) [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] 3. Find N'th Most Common Key-Vaue Pairs Counter('abracadabra').most_common(3) [('a', 5), ('b', 2),..