首页 > 解决方案 > 如何打印句子中的特定单词

问题描述

我试图在我的英语课上工作,我试图从一个句子中取出一个特定的代词,所以我想选择这样的代词:示范和使用 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 开头的单词,但我想要像代词这样的特定单词,所以请帮忙

标签: python

解决方案


当我编写一些更简单的代码可以让我知道代词的数量并且我要求答案时,我找到了答案,所以有人帮助我这里是代码:

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 那就是我得到帮助的地方。


推荐阅读