There are many way for Counting the occurrence of unique letters in a string in Python 3. I just wanna know what is the best way for this job. Thanks.
Asked
Active
Viewed 3,294 times
-4
-
3Welcome to Stack Overflow. If you are aware of the many ways to count the occurrences of unique letters, you should be able to write a benchmark that compares them in order to determine which is best. This is not a homework completion service. – Ken White Sep 05 '17 at 04:38
-
Please take a moment to read these- https://stackoverflow.com/help/mcve, https://stackoverflow.com/help/how-to-ask – Souvik Ghosh Sep 05 '17 at 04:49
2 Answers
1
if you need to count characters in string, try the following
a = "aaabbcccd"
b = dict.fromkeys(a, 0)
for i in a:
b[i] += 1
b now holds the counts you require:
{'a': 3, 'c': 3, 'b': 2, 'd': 1}

Heemanshu Bhalla
- 3,603
- 1
- 27
- 53

BA.
- 924
- 7
- 10
-1
try:
''.join(set('aaabbbcccdddd'))
or:
list(set('aaabbbcccdddd'))
Also see the post: List of all unique characters in a string?

BA.
- 924
- 7
- 10