0

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?

1
  • Did you try to see what happens? Commented Oct 17, 2015 at 19:22

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

Comments

0
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.