简体   繁体   English

用于检测以字符串中的大写字母开头的所有单词的代码

[英]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 . 我正在写一个小片段,它抓住所有以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. 你只评估它一次,所以你得到一个True列表,它只附加最后一项。

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: 您可以利用filter功能:

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. 我编写了以下python片段,将大写字母的起始单词存储到字典中作为键,而不是将其作为该字典中的值出现在键中。

#!/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 对于上述代码的性能或简单性的任何改进建议都是受欢迎的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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