-
-
Save IndhumathyChelliah/3bf7a2cf3a5abf0a15f9721467abdd13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#finding the number of occurrences of each character in the string | |
s="dictionary" | |
d={n:s.count(n) for n in s} | |
print (d)#Output:{'d': 1, 'i': 2, 'c': 1, 't': 1, 'o': 1, 'n': 1, 'a': 1, 'r': 1, 'y': 1} | |
#finding the number of occurrences of each word in list using dict comprehension | |
l=["red","green","blue","red"] | |
d={n:l.count(n) for n in l} | |
print (d)#Output:{'red': 2, 'green': 1, 'blue': 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment