[파이썬] collection 모듈

collection 모듈

collection.Counter()

컨테이너에 동일한 값의 자료가 몇개인지를 파악하는데 사용하는 객체이다.

1. counter() 에 입력되는 입력값

1) 리스트(list)

2) 딕셔너리(dictionary)

import collection
dic = {'가': 1 , '나': 2 , '다' : 3, '라' : 4}
print(collections.Counter(dic))
---------------------------------------------------
출력값 = Counter({'라': 4, '다': 3, '나': 2, '가': 1})

3) 값 = 개수 형태

c = collections.Counter(a=2, b=3, c=2)  # 값 = 개수 형태로 입력
print(collections.Counter(c))
print(sorted(c.elements()))  # elements() 함수 사용, sorted를 이용
print(c.elements()) # sorted 를 안하면 값이 출력안됨

---------------------------------------------------------
Counter({'b': 3, 'a': 2, 'c': 2})
['a', 'a', 'b', 'b', 'b', 'c', 'c']
<itertools.chain object at 0x0000016449BB9C50>

4) 문자열 (string)

c = collections.Counter("안녕하신가요, 안녕하세요")       # 한글도 가능
print(c)             # collections.Counter 에 설정되는 방식  
--------------------------------------------------------
Counter({'안': 2, '녕': 2, '하': 2, '요': 2, '신': 1, '가': 1, ',': 1, ' ': 1, '세': 1})

2. Counter 의 메소드

1) update()

c = collections.Counter('ababab')
print(c)
c.update("ccffxx")
print(c)
c.update({'f': 1, 'x' : 5, 'z' : 100})
print(c)
---------------------------------------------------------
Counter({'a': 3, 'b': 3})
Counter({'a': 3, 'b': 3, 'c': 2, 'f': 2, 'x': 2})
Counter({'z': 100, 'x': 7, 'a': 3, 'b': 3, 'f': 3, 'c': 2})

2) elements()

c = collection.Counter('hellow python')
print(list(c.elements()))
print(sorted(c.elements()))
-----------------------------------------------------------
['h', 'h', 'e', 'l', 'l', 'o', 'o', 'w', ' ', 'p', 'y', 't', 'n']
[' ', 'e', 'h', 'h', 'l', 'l', 'n', 'o', 'o', 'p', 't', 'w', 'y']
## 띄어 쓰기도 포함

3) most_common(n)

c2 = collections.Counter('apple, orange, grape')
print(c2.most_common())
print(c2.most_common(3))
-----------------------------------------------------------
[('a', 3), ('p', 3), ('e', 3), (',', 2), (' ', 2), ('r', 2), ('g', 2), ('l', 1), ('o', 1), ('n', 1)]
[('a', 3), ('p', 3), ('e', 3)]

4) subtrant()

c3 = collections.Counter('hello python')
c4 = collections.Counter('i love python')
c3.subtract(c4)
print(c3)


t = collections.Counter(a=4, b=2, c=0, d=-2)
d = collections.Counter(a=1, b=2, c=3, d=4)
t.subtract(d)
print(c)

------------------------------------------------------------
Counter({'h': 1, 'l': 1, 'e': 0, 'o': 0, 'p': 0, 'y': 0, 't': 0, 'n': 0, ' ': -1, 'i': -1, 'v': -1})
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})  # t-d
a = collections.Counter('aabbccdd')
b = collections.Counter('aabbbce')
print(a & b)