[英]how can i find the words in a string that start with capital letter?
How can I find the words in a string that start with a capital letter?如何找到以大写字母开头的字符串中的单词?
Example input:示例输入:
input_str = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace."
Expected output:预期 output:
Persian League Iran Persian League
Assuming you can accept The
and This
as well:假设您也可以接受
The
和This
:
import re
input_string = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace."
matches = re.findall("([A-Z].+?)\W", input_string)
gives给
['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League', 'This']
If you need to ignore The
and This
:如果您需要忽略
The
和This
:
matches = re.findall("(?!The|This)([A-Z].+?)\W", input_string)
gives给
['Persian', 'League', 'Iran', 'Persian', 'League']
Without regex:没有正则表达式:
txt = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship."
print([w for w in txt.split() if w.istitle()])
Output: Output:
['The', 'Persian', 'League', 'Iran.', 'The', 'Persian', 'League']
If you want to skip the The
word (or any other word for that matter) try this:如果你想跳过
The
词(或任何其他词)试试这个:
print(" ".join(w.replace(".", "") for w in txt.split() if w[0].isupper() and w not in ["The", "This"]))
Output: Output:
Persian League Iran Persian League
s = """
The Persian League is the largest sport event dedicated to the deprived areas
of Iran. The Persian League promotes peace and friendship. This video was
captured by one of our heroes who wishes peace.
"""
print( [ x for x in s.split() if x[0].isupper() ])
Try this:尝试这个:
import re
inputString = "The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship."
splitted = re.split(' |\.', inputString)
result = filter(lambda x: len(x) > 0 and x[0].isupper(), splitted)
print(list(result))
Result:结果:
['The', 'Persian', 'League', 'Iran', 'The', 'Persian', 'League']
Another way to solve is using for
to read data and put the words with capital letters in a list.另一种解决方法是使用
for
读取数据并将带有大写字母的单词放入列表中。
phrase = 'The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace.'
wordsplit = phrase.split(' ')
capitalLettersWords = []
for word in wordsplit:
if word[0].isupper():
capitalLettersWords.append(word)
print(capitalLettersWords)
#['The', 'Persian', 'League', 'Iran.', 'The', 'Persian', 'League', 'This']
In my example I used the str.isupper() and str.split() , both built-in methods from Python standard lib.在我的示例中,我使用了str.isupper()和str.split() ,它们都是 Python 标准库中的内置方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.