用Python编写一个程序,在文本输出中打印关键字(以大写字母开头的单词)以及字号(多个单词)。如果在文本中找不到具有此属性的单词,请在“无”输出中打印它。句子开头的单词不应被视为索引词。(从1开始输入字号)
除索引词外,不计算数字。除了句号外,句子中唯一使用的符号是逗号。如果分号位于单词末尾,请务必将其删除
输入 波斯联盟是专门为伊朗贫困地区举办的最大的体育赛事。波斯联盟促进和平与友谊。 这段视频是我们的一位希望和平的英雄拍摄的
输出强文本 2:波斯语 3:联盟 15:伊朗 17:波斯语 18:联盟
如何修复它?://strong文本
enter code here
import re
inputText = ""
# we will use this regex pattern to check if a word is started with upperCase
isUpperCase = re.compile("^([A-Z])\w+")
# we will store upperCase words in this array
result = []
# number of word in the hole input
wordIndex = 0
# separate sentences
sentences = inputText.strip().split('.')
for s in sentences:
# get array of words in each sentence
words = s.strip().split(' ')
for index, word in enumerate(words):
# increase wordIndex
wordIndex += 1
# just pass first word
if index == 0:
continue
# check regex and if true add word and wordIndex to result
if isUpperCase.match(word):
result.append({
"index": wordIndex,
"word": word
})
# finally print result
for word in result:
print(word["index"], ": ", word["word"])
您可以将每个大写单词及其索引值(加一)添加到字典中。我注意到您没有返回
The
或This
,但我不知道规则是什么,所以我只是为这两个词添加了一个豁免相关问题 更多 >
编程相关推荐