简体   繁体   中英

Code to detect all words that start with a capital letter in a string

I'm writing out a small snippet that grabs all letters that start with a capital letter in python . Here's my code

def WordSplitter(n):
    list1=[]
    words=n.split()
    print words

    #print all([word[0].isupper() for word in words])
    if ([word[0].isupper() for word in words]):
        list1.append(word)
    print list1

WordSplitter("Hello How Are You")

Now when I run the above code. Im expecting that list will contain all the elements, from the string , since all of the words in it start with a capital letter. But here's my output:

@ubuntu:~/py-scripts$ python wordsplit.py 
['Hello', 'How', 'Are', 'You']
['You']# Im expecting this list to contain all words that start with a capital letter

You're only evaluating it once, so you get a list of True and it only appends the last item.

print [word for word in words if word[0].isupper() ]

or

for word in words:
    if word[0].isupper():
        list1.append(word)

You can take advantage of the filter function:

l = ['How', 'are', 'You']
print filter(str.istitle, l)

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
        x = re.search(r"[A-Z]\S+", word)
        if x:
        #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 shown both ways, the array index to check for the uppercase start letter and by using the regular expression. Anymore improvement suggestion for the above code for performance or for simplicity is welcome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM