Python - Collection의 Counter 에 관한 공부
# Collection.Counter 공부
# Collection.Counter는 이름 그대로 갯수룰 count해주는 것이다.
import collections
"""
1. 배열 안 요소의 갯수를 카운트해준다.
- 리스트에 출현한 요소의 값과 반복 횟수가 Key, value 방식으로 나타난다.
- 리턴값은 dict의 subclass로 dict에 사용할 수 있는 메소드는 모두 사용가능하다.
"""
participant = ["hello", "world", "nice", "to", "meet", "you", "hello", "hello", "hello"]
counted = collections.Counter(participant)
print(counted)
# result : Counter({'hello': 4, 'world': 1, 'nice': 1, 'to': 1, 'meet': 1, 'you': 1})
"""
2. value가 큰 값을 리턴해준다.
"""
sample = dict(small=1, bigger=10)
example = dict(hello="world", goodbye="dear", welcome="sorld")
print(collections.Counter(sample)) # result : Counter({'bigger': 10, 'small': 1})
print(collections.Counter(example)) # result : Counter({'hello': 'world', 'welcome': 'sorld', 'goodbye': 'dear'})
"""
3. 반대로 카운터 객체에 파라미터와 해당 파라미터의 반복 횟수를 전달해서 list를 얻을 수 있다.
- 단, list로 wrapping 해주어야 한다. 그렇지 않으면 iterator가 출력 됨.
"""
sample = collections.Counter(a=2, b=5, c=7)
example = collections.Counter(a=2, b=4, c=7)
print(sample) # Counter({'c': 7, 'b': 5, 'a': 2})
print(list(sample.elements())) # ['a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c']
"""
4. Counter끼리는 서로 수학처럼 덧셈/뺄셈/합집합/교집합 연산이 가능하다.
- Counter 객체끼리 더하거나 빼면 '파라미터':'연산 후 횟수' 가 리턴된다.
- 값이 거의 같은 리스트를 이것으로 비교하면 어떤 리스트가 항목을 가지고 있는지 쉽고 빠르게 알 수 있다.
"""
sample = collections.Counter(a=2, b=5, c=7)
example = collections.Counter(a=2, b=4, c=7)
print(sample-example) # Counter({'b': 1})
print(sample&example) # Counter({'c': 7, 'b': 4, 'a': 2})
print(sample|example) # Counter({'c': 7, 'b': 5, 'a': 2})
participant = collections.Counter(["hello", "world", "goodbye", "mydear"])
completion = collections.Counter(["hello", "world", "goodbye"])
print(participant + completion) # Counter({'mydear': 1})
# Collection.Counter는 이름 그대로 갯수룰 count해주는 것이다.
import collections
"""
1. 배열 안 요소의 갯수를 카운트해준다.
- 리스트에 출현한 요소의 값과 반복 횟수가 Key, value 방식으로 나타난다.
- 리턴값은 dict의 subclass로 dict에 사용할 수 있는 메소드는 모두 사용가능하다.
"""
participant = ["hello", "world", "nice", "to", "meet", "you", "hello", "hello", "hello"]
counted = collections.Counter(participant)
print(counted)
# result : Counter({'hello': 4, 'world': 1, 'nice': 1, 'to': 1, 'meet': 1, 'you': 1})
"""
2. value가 큰 값을 리턴해준다.
"""
sample = dict(small=1, bigger=10)
example = dict(hello="world", goodbye="dear", welcome="sorld")
print(collections.Counter(sample)) # result : Counter({'bigger': 10, 'small': 1})
print(collections.Counter(example)) # result : Counter({'hello': 'world', 'welcome': 'sorld', 'goodbye': 'dear'})
"""
3. 반대로 카운터 객체에 파라미터와 해당 파라미터의 반복 횟수를 전달해서 list를 얻을 수 있다.
- 단, list로 wrapping 해주어야 한다. 그렇지 않으면 iterator가 출력 됨.
"""
sample = collections.Counter(a=2, b=5, c=7)
example = collections.Counter(a=2, b=4, c=7)
print(sample) # Counter({'c': 7, 'b': 5, 'a': 2})
print(list(sample.elements())) # ['a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c']
"""
4. Counter끼리는 서로 수학처럼 덧셈/뺄셈/합집합/교집합 연산이 가능하다.
- Counter 객체끼리 더하거나 빼면 '파라미터':'연산 후 횟수' 가 리턴된다.
- 값이 거의 같은 리스트를 이것으로 비교하면 어떤 리스트가 항목을 가지고 있는지 쉽고 빠르게 알 수 있다.
"""
sample = collections.Counter(a=2, b=5, c=7)
example = collections.Counter(a=2, b=4, c=7)
print(sample-example) # Counter({'b': 1})
print(sample&example) # Counter({'c': 7, 'b': 4, 'a': 2})
print(sample|example) # Counter({'c': 7, 'b': 5, 'a': 2})
participant = collections.Counter(["hello", "world", "goodbye", "mydear"])
completion = collections.Counter(["hello", "world", "goodbye"])
print(participant + completion) # Counter({'mydear': 1})
댓글
댓글 쓰기