[英]How to print specific words from a sentence
我試圖在我的英語課上工作,我試圖從一個句子中取出一個特定的代詞,所以我想選擇這樣的代詞:demostrative 並使用 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 那就是我得到幫助的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.