Hi,
I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key.
#!/usr/bin/env python
import sys
import re
hash = {} # initialize an empty dictinonary
for line in sys.stdin.readlines():
for word in line.strip().split(): # removing newline char at the end of the line
#l = re.search("\b[A-Z]\S+", word)
if word[0].isupper():
if word in hash:
hash[word] += 1
else:
hash[word] = 1
for word, cnt in hash.iteritems(): # iterating over the dictionary items
sys.stdout.write("%d %s\n" % (cnt, word))
In the above code, I am using the array index to check for the uppercase. But when I tried to re-code it using the regular expression as:
l = re.search("\b[A-Z]\S+", word)
that is not fetching me the anticipated output. Expecting your help in this regard.