How can we determine the occurence of each word in a string using python | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How can we determine the occurence of each word in a string using python

No inbuilt function to be used

23rd Aug 2020, 3:40 PM
Chahal Vadalmiya
Chahal Vadalmiya - avatar
2 Antworten
+ 1
Maintain a empty dictionary and variable count initialized to 0 ,loop over each character in a string , check if that character is in dictionary or not, if it is not there increment count by 1 for indicating it's first occurence and insert key as alphabet and and value as count ,if character is in dictionary ,store that dictionary character value to a variable like a=dictionary[character] increment the value a+=1 insert the new value back into dictionary dictionary[character]=a
23rd Aug 2020, 3:53 PM
Abhay
Abhay - avatar
0
You can also try collection for Easy way from collections import Counter S = Counter(yourString) Print(S) This will help
23rd Aug 2020, 5:37 PM
Hemu@coder