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

see*_*ker 5 python

我正在写一个小片段,它抓住所有以python中的大写字母开头的字母.这是我的代码

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")
Run Code Online (Sandbox Code Playgroud)

现在我运行上面的代码.我希望该列表将包含字符串中的所有元素,因为其中的所有单词都以大写字母开头.但这是我的输出:

@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
Run Code Online (Sandbox Code Playgroud)

Jor*_*ley 9

你只评估它一次,所以你得到一个True列表,它只附加最后一项.

print [word for word in words if word[0].isupper() ]
Run Code Online (Sandbox Code Playgroud)

要么

for word in words:
    if word[0].isupper():
        list1.append(word)
Run Code Online (Sandbox Code Playgroud)