简体   繁体   中英

How can I count the number of each character in a Python string?

I have written this Python program to count the number of each character in a Python string.

def count_chars(s):
    counts = [0] * 65536
    for c in s:
        counts[ord(c)] += 1
    return counts

def print_counts(counts):
    for i, n in enumerate(counts):
        if n > 0:
            print(chr(i), '-', n)

if __name__ == '__main__':
    print_counts(count_chars('hello, world \u2615'))

Output:

  - 2
, - 1
d - 1
e - 1
h - 1
l - 3
o - 2
r - 1
w - 1
☕ - 1

Can this program take care of counting the number of any occurrences of any Unicode character? If not, what can be done to ensure that every possible Unicode character is taken care of?

Your code only handles characters in the Basic Multilingual Plane ; emoticons won't be handled, for example. You could remedy that by just using a dictionary instead of a list with a fixed number of indices, and use the characters as keys.

However, you should just use a collections.Counter() object :

from collections import Counter

counts = Counter(s)

for character, count in counts.most_common():
    print(character, '-', count)

It is, after all, designed for just such use cases.

Demo:

>>> from collections import Counter
>>> s = 'hello, world \u2615 \U0001F60A'
>>> counts = Counter(s)
>>> for character, count in counts.most_common():
...     print(character, '-', count)
...
  - 3
l - 3
o - 2
r - 1
w - 1
e - 1
h - 1
d - 1
☕ - 1
, - 1
😊 - 1
message='alpha beta gamma sudama'
z = list(message)
p = []
for x in range (0,len(z)):
    y=0
    i=0
    count=0
    if z[x] not in p:
        p.append(z[x])
        while i < len(z) :
            if z[x] == z[i]:
                count = count+1
            i = i+1
        print(z[x],count)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM