what i'm finding hard is that you can't nest "i for i in [list]" inside Boolean logic checks, and not wanting to list all 26 characters
-
2[`collections.Counter("ababac")`](https://docs.python.org/3/library/collections.html#collections.Counter)? – jonrsharpe Jan 04 '19 at 12:17
-
3Possible duplicate of [Counting Letter Frequency in a String (Python)](https://stackoverflow.com/questions/40985203/counting-letter-frequency-in-a-string-python) – pmcarpan Jan 04 '19 at 12:24
3 Answers
You can use "aabc".count('a')
to give you the amount of times a
appears in aabc
, for example. In this example, it gives 2
.
Building on from this, you could implement some simple code to check how many times every letter of the alphabet occurs like so:
import string
s = 'ababac'
for letter in string.ascii_lowercase:
print("%s appears %s times" % (letter, s.count(letter)))
Which produces the output:
a appears 3 times
b appears 2 times
c appears 1 times
d appears 0 times
e appears 0 times
f appears 0 times
g appears 0 times
...
NB: you don't have to list all 26 characters in the alphabet because string.ascii_lowercase
has that for you. Note there is also string.ascii_uppercase
and so on. More information: https://docs.python.org/3/library/string.html
For more information about str.count(sub[, start[, end]])
: https://docs.python.org/3/library/stdtypes.html#str.count
Also collections.Counter
can create a dictionary for all the letters in the string. Using this, you could rewrite the above code like so:
import string
from collections import Counter
counts=Counter('ababac')
# counts = {'a': 3, 'b': 2, 'c': 1}
for letter in string.ascii_lowercase:
if letter in counts:
print("%s appears %s times" % (letter, counts[letter]))
else:
print("%s doesn't appear" % letter)
Output:
a appears 3 times
b appears 2 times
c appears 1 times
d doesn't appear
e doesn't appear
f doesn't appear
g doesn't appear
...
Edit NB:
print("%s appears %s times" % (letter, s.count(letter)))
can also be written as:
print("{} appears {} times".format(letter, s.count(letter)))
using the newer str.format
interface. More information: https://docs.python.org/3/library/stdtypes.html#str.format.
-
thanks! this solves my problem, but I don't get how: `("%s appears %s times" % (letter, s.count(letter)))` works. where can I check about this specific string usage? – Snippyro Jan 04 '19 at 12:46
-
It's called string formatting. Some more information: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting. There are actually a newer and better ways to do this too nowadays, more information about another way: https://docs.python.org/3/library/stdtypes.html#str.format – Jan 04 '19 at 12:53
using defualtdict
you can do this way .
from collections import defaultdict
def fun():
return 0
dic=defaultdict(fun)
string ='ababa'
for i in string:
dic[i]+=1
for i in dic:
print(i,dic[i])
output:
a 3
b 2

- 10,069
- 3
- 29
- 44
-
rather than doing `defaultdict(fun)` you could use a lambda method like so `defaultdict(lambda: 0)`, which means you don't need to define `fun` – Jan 04 '19 at 12:36
-
@AbhinavBhandari i know, my main motive is to let the OP to understand the code and solve it in future and also no use of any library – sahasrara62 Jan 04 '19 at 13:55
You can get the each available characters count in a given string using below code
def count_dict(mystring):
d = {}
# count occurances of character
for w in mystring:
d[w] = mystring.count(w)
# print the result
for k in sorted(d):
print (k + ': ' + str(d[k]))
mystring='qwertyqweryyyy'
count_dict(mystring)
You can find the live demo here

- 5,547
- 8
- 20
- 42
-
1Suraj, you're already linking to the demo in your answer, there is no need to attempt to edit the link into prashant's answer as well: https://stackoverflow.com/review/suggested-edits/21843571 – Nick is tired Jan 04 '19 at 12:40