python - 如何打印句子中的特定单词
问题描述
我试图在我的英语课上工作,我试图从一个句子中取出一个特定的代词,所以我想选择这样的代词:示范和使用 if 语句只打印句子中的示范代词,我只能找到这个:
**# initializing list
test_list = ["There is a weekend tomorrow", "This is a simple String", "This is only for demonstration of what i got", "Please help"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = "T"
res = [ele for temp in test_list for ele in temp.split() if ele[0].lower() == K.lower()]
# printing result
print("The filtered elements : " + str(res))**
它只打印以 T 开头的单词,但我想要像代词这样的特定单词,所以请帮忙
解决方案
当我编写一些更简单的代码可以让我知道代词的数量并且我要求答案时,我找到了答案,所以有人帮助我这里是代码:
print("Noun Counter")
sentence = input("Write A sentence: ")
pronoun = (input("Choose a pronoun from Demonstrative, interrogative and indefinite: "))
if pronoun == "Demonstrative":
source = sentence
pronouns = ["this", "that", "these", "those"]
print("Number of Demonstrative pronouns: ")
count = dict(zip(pronouns, [0]*len(pronouns))) # addition
words = source.split()
for w in words:
if w.lower() in pronouns:
count[w.lower()] += 1 # addition
print(count)
elif pronoun == "Indefinite":
source = sentence
pronouns = ["Another", "anybody", "anyone", "anything", "each", "either", "enough", "everybody", "everyone",
"everything", "little", "much", "neither", "nobody", "no one", "nothing", "one", "other", "somebody",
"something", "Both", "few", "fewer", "many", "others", "several", "All", "any", "more", "most",
"someone", "none", "some", "such"] # download a list from somewhere
print("Number of Indefinite pronouns: ")
count = dict(zip(pronouns, [0]*len(pronouns))) # addition
words = source.split()
for w in words:
if w.lower() in pronouns:
count[w.lower()] += 1 # addition
print(count)
elif pronoun == "Interrogative":
source = sentence
pronouns = ["who", "whom", "whose", "what", "which"]
print("Number of Interrogative pronouns: ")
count = dict(zip(pronouns, [0]*len(pronouns))) # addition
words = source.split()
for w in words:
if w.lower() in pronouns:
count[w.lower()] += 1 # addition
print(count)
else:
print("No pronoun found named: " + pronoun + ", please try again")
无论如何,感谢您的所有帮助。无论它在哪里说#addition 那就是我得到帮助的地方。
推荐阅读
- angular - Angular/NativeScript 应用程序中的单元测试
- powershell - 如何使用输入文件来提供 powershell 命令?
- mysql - 如何使用 sequelize mysql 添加外键
- typescript - 使用枚举作为字典键
- python - 在 python2.7 环境中 conda install basemap 给出运行时错误 R6034
- highcharts - Trying to figure out how to assign specific colors to specific data points in Highchart Pie chart
- sql-server - SQL Server dynamic SQL to query on sys tables
- html - 连续对齐具有不同尺寸的图像
- linux - Putty Search - String followed by one but not two symbols
- javascript - 将所有状态属性传递给函数,除了一个