Get the number of occurrences of each character
Question:
Given the string:
a='dqdwqfwqfggqwq'
How do I get the number of occurrences of each character?
Answers:
In 2.7 and 3.1 there’s a tool called Counter:
>>> import collections
>>> results = collections.Counter("dqdwqfwqfggqwq")
>>> results
Counter({'q': 5, 'w': 3, 'g': 2, 'd': 2, 'f': 2})
Docs. As pointed out in the comments it is not compatible with 2.6 or lower, but it’s backported.
For each letter count the difference between string with that letter and without it, that way you can get it’s number of occurences
a="fjfdsjmvcxklfmds3232dsfdsm"
dict(map(lambda letter:(letter,len(a)-len(a.replace(letter,''))),a))
Not highly efficient, but it is one-line…
In [24]: a='dqdwqfwqfggqwq'
In [25]: dict((letter,a.count(letter)) for letter in set(a))
Out[25]: {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
lettercounts = {}
for letter in a:
lettercounts[letter] = lettercounts.get(letter,0)+1
one line code for finding occurrence of each character in string.
for i in set(a):print(‘%s count is %d’%(i,a.count(i)))
You can do this:
listOfCharac={}
for s in a:
if s in listOfCharac.keys():
listOfCharac[s]+=1
else:
listOfCharac[s]=1
print (listOfCharac)
Output {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
This method is efficient as well and is tested for python3.
python code to check the occurrences of each character in the string:
word = input("enter any string = ")
for x in set(word):
word.count(x)
print(x,'= is', word.count(x))
Please try this code if any issue or improvements please comments.
Given the string:
a='dqdwqfwqfggqwq'
How do I get the number of occurrences of each character?
In 2.7 and 3.1 there’s a tool called Counter:
>>> import collections
>>> results = collections.Counter("dqdwqfwqfggqwq")
>>> results
Counter({'q': 5, 'w': 3, 'g': 2, 'd': 2, 'f': 2})
Docs. As pointed out in the comments it is not compatible with 2.6 or lower, but it’s backported.
For each letter count the difference between string with that letter and without it, that way you can get it’s number of occurences
a="fjfdsjmvcxklfmds3232dsfdsm"
dict(map(lambda letter:(letter,len(a)-len(a.replace(letter,''))),a))
Not highly efficient, but it is one-line…
In [24]: a='dqdwqfwqfggqwq'
In [25]: dict((letter,a.count(letter)) for letter in set(a))
Out[25]: {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
lettercounts = {}
for letter in a:
lettercounts[letter] = lettercounts.get(letter,0)+1
one line code for finding occurrence of each character in string.
for i in set(a):print(‘%s count is %d’%(i,a.count(i)))
You can do this:
listOfCharac={}
for s in a:
if s in listOfCharac.keys():
listOfCharac[s]+=1
else:
listOfCharac[s]=1
print (listOfCharac)
Output {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
This method is efficient as well and is tested for python3.
python code to check the occurrences of each character in the string:
word = input("enter any string = ")
for x in set(word):
word.count(x)
print(x,'= is', word.count(x))
Please try this code if any issue or improvements please comments.