-1

I want to count how many characters are there in a string but not occurences. For example:

test_list=("aabbccddee")

I want the result to be 5 because there are 5 characters:

(a,b,c,d,e) 

I tried using len function and count and also

from collections import defaultdict
Cow
  • 2,543
  • 4
  • 13
  • 25
  • Does this answer your question? [Count the number of occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-of-occurrences-of-a-character-in-a-string) – コリン Dec 21 '22 at 07:55

4 Answers4

3

Does this solve your problem?

from collections import Counter

given_string = "aabbccddee"
result = Counter(given_string)
print(len(result))
fenderogi
  • 118
  • 7
3

Use set in python.

len(set("aabbccddee"))
# returns 5

sets in Python do not contain repeated members (nor are they ordered) like sets in math. Another syntax to define a set is like a dictionary without keys:

set_example = { 1, 2, 2, 3, 3, 3 }
# set_example is { 1, 2, 3 }
BSimjoo
  • 149
  • 8
1

Try this one:

your_str = 'aabbccddee'

result = len(set(your_str))
print(result)
0

An alternative to set():

len(dict.fromkeys(test_list))
#5
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44