파이썬 라이브러리

조합,순열

순열

from itertools import permutations

print(list(permutations([1,2,3,4], 2)))
print(list(permutations([1,2,3,1], 2)))

# result1
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

# result2
# [(1, 2), (1, 3), (1, 1), (2, 1), (2, 3), (2, 1), (3, 1), (3, 2), (3, 1), (1, 1), (1, 2), (1, 3)]

조합

from itertools import combinations

print(list(combinations([1,2,3,4], 2)))
print(list(combinations([1,2,3,1], 2)))

# result1
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

# result2
# [(1, 2), (1, 3), (1, 1), (2, 3), (2, 1), (3, 1)]

카운터

from collections import counter
키,값으로 값에 키에 대한 횟수로 바꿔준다


Counter('hello world') # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

좋은 웹페이지 즐겨찾기