프로그래밍/Python
[Python] collection 모듈의 Counter 객체
코딩하는 문과생
2019. 11. 17. 20:10
리스트끼리 '차'는 collections 모듈의 Counter객체를 통해 가능하다. 결과는 딕셔너리다.
집합은 차집합 연산이 기본적으로 제공된다.
import collections as col
a=[1,2,3,4,5]
b=[2,3,5]
print(col.Counter(a)-col.Counter(b))
#Counter({1: 1, 4: 1})
######################################3
c={'a', 'b', 'c', 'd'}
d={'a', 'c'}
print(c-d)
#{'d', 'b'}