counter 및 count 예제

import collections
myList=['a','b','c','a','b','d']
myCounter=collections.Counter(myList)
myCounter
  Counter({'a': 2, 'b': 2, 'c': 1, 'd': 1})
print(myCounter['a'])
  2

yourList=['a','b','b','e']
ourList=myList+yourList
ourList
  ['a', 'b', 'c', 'a', 'b', 'd', 'a', 'b', 'b', 'e']
type(ourList)
  list
ourCounter=collections.Counter(ourList)
ourCounter
  Counter({'a': 3, 'b': 4, 'c': 1, 'd': 1, 'e': 1})
ourCounter.most_common(2)
  [('b', 4), ('a', 3)]
ourCounter.keys()
  dict_keys(['a', 'b', 'c', 'd', 'e'])
ourCounter.items()
  dict_items([('a', 3), ('b', 4), ('c', 1), ('d', 1), ('e', 1)])
import itertools
for i in itertools.count(1,1): # 1부터 시작하여 1씩 증가 하는 카운터
    print(i)
    if i>10 :
        break
  1
2
3
4
5
6
7
8
9
10
11

k=itertools.count(1)
print(k)
  count(1)

print(k)
  count(1)

k
  count(1)
print(iter(k),iter(k))
  count(1) count(1)

iter(k)
  count(1)

Tags:

Updated:

Leave a Comment