Terry Very Good

[Python] 문자열에서 중복된 문자 or 단어 카운트 본문

프로그래밍/PYTHON

[Python] 문자열에서 중복된 문자 or 단어 카운트

테리베리 2021. 6. 15. 09:44
728x90
반응형
s = 'life is short, so python is easy'

punct = ' ,.' # 카운트에서 뺄 단어
d= {}
for c in s:
    if c in punct:
        continue
    d[c] = d.get(c,0) + 1
d

결과: {'l': 1, 'i': 3, 'f': 1, 'e': 2, 's': 5, 'h': 2, 'o': 3, 'r': 1, 't': 2, 'p': 1, 'y': 2, 'n': 1, 'a': 1}

punct = ' ,.'
d= {}
for c in s.split(' '):
    if c in punct:
        continue
    d[c] = d.get(c,0) + 1
d

결과: {'life': 1, 'is': 2, 'short,': 1, 'so': 1, 'python': 1, 'easy': 1}

728x90
반응형