파이썬 내장함수

오민석·2021년 3월 18일
0

dictionary

dict = {'A' :1,'D' :4,'C' :3,'B' :2}

정렬 -key : sorted(dict.items())
-value : import operator
sorted(dict.items(), key=operator.itemgetter(1))

key,values,items
print :for key,value in dict.items():
print(key,value)

Counter

from collections import Counter

s="{{20,111},{111}}"
a=Counter(s) : Counter({'{': 3, '2': 1, '0': 1, ',': 2, '1': 6, '}': 3})
a.most_common(n=2) : [(',', 9), ('{', 5)]

print :for i,j in dict.items():
print(i,j)

sort

sorted(list, key=lambda x : x[1])
sorted(list, key=lambda x : (x[1],x[1]))

permutation, combination

from itertools import permutations
a = [1,2,3]

permute = permutations(a,2)
print(list(permute))
-> [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

from itertools import combinations
combi = combinations(a,2)
print(list(combi))
-> [(1, 2), (1, 3), (2, 3)]

from itertools import product
list(product(a,repeat=2))
-> [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

map

2차원 리스트 최소, 최대값
min(map(min,data)), max(map(max,data))

[(i,j) for i in range(n) for j in range(n) if data[i][j]==2]

rjust, ljust, zfill

"2".rjust(3,"0")
#"002"

"123".ljust(5,"1")
#"12311"

"2".zfill(3)
#"002"

"123".zfill(8)
#"000000123"

0개의 댓글