단어를 이루는 알파벳의 갯수를 dict
형식으로 반환함.
from collections import Counter
print(Counter('hello'))
#Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
print(Counter('hello').items())
#dict_items([('h', 1), ('e', 1), ('l', 2), ('o', 1)])
def filter_unique(word) :
return [item for item, count in Counter(word).items()
if count > 1]
filter_unique('apple')
#['p']
alist = []
def filter_non_unique(word) :
counter = Counter(word)
for word in counter :
if counter[word] == 1:
alist.append(word)
return alist
filter_non_unique('hello')
#['h', 'e', 'o']
Counter
의 key
, value
값을 나눠서 사용할 수 있다.key
값이 나온다. -> value
값을 counter[word]의 형식으로 나타낼 수 있음🍟
Counter
print(Counter('hello') #Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
value
값을 기준으로 내림차순 배열된다.🍟
items.()
(리스트안의 튜플)print(Counter('hello').items()) #dict_items([('h', 1), ('e', 1), ('l', 2), ('o', 1)])
🍟
dictionary
print(dict(Counter('hello'))) #{'h': 1, 'e': 1, 'l': 2, 'o': 1}
🍟
list
print(list(Counter('hello'))) #['h', 'e', 'l', 'o']
key
값만 남는다.
from collections import Counter
def max_find(word):
finder = Counter(word).items()
max_number = 0
for key, value in finder :
if value > max_number :
max_number = value
max_alpha = key
return max_alpha, max_number
print(max_find('helllllllo'))
#('l', 7)
from collections import Counter
def max_find(word):
finder = Counter(word)
max_number = 0
for x in finder :
if finder[x] > max_number :
max_number = finder[x]
max_alpha = x
return max_alpha, max_number
print(max_find('hello july'))
#('l', 3)