gpt4 book ai didi

How to get lower function working to find string in list(如何让下级函数在列表中查找字符串)

转载 作者:bug小助手 更新时间:2023-10-22 18:18:46 26 4
gpt4 key购买 nike



I'm a student learning python and am currently working on an assignment for my class. I am running into a problem with trying to use the lower function to find user input string in a list. Here's the code so far

我是一名学习python的学生,目前正在为我的班级做作业。我在尝试使用lower函数在列表中查找用户输入字符串时遇到了一个问题。这是迄今为止的代码


#Create list of fruits
fruits = ['banana' , 'apple' , 'orange' , 'kiwi' , 'pineapple', 'peach', 'papaya']

#Welcome user and explain fruit finder
welcome = "Welcome to the fruit finder program, please enter your name: "
sentence = input('Please enter a sentence with fruit in it to begin: ')

#Check if input matches fruit list and display the number of fruits to user
if sentence.lower() in fruits:
print("Here are the fruits found in your sentence: ")
else:
print(input('Fruit not found in sentence. Please try again: '))

The output I get is as follows:

我得到的输出如下:



Please enter a sentence with fruit in it to begin: Banana Kiwi Pineapple
Fruit not found in sentence. Please try again:



更多回答

Your current example will look for the entire sentence banana kiwi pineapple, which isn't in the fruits list.

您当前的示例将查找整个句子香蕉猕猴桃菠萝,它不在水果列表中。

优秀答案推荐

Apart from the fact that some indentation is off you need to:

除了某些缩进已关闭之外,您还需要:



  • split the input, probably by space " " as the separator

  • lower each word in the input

  • iterate over words


Here's the example if you want to print them one by one if not you can aggregate the results in a list, set, dictionary (to assign counts as well), whatever you need.

下面是一个例子,如果你想一个接一个地打印它们,如果不想,你可以将结果聚合到列表、集合、字典中(也可以分配计数),无论你需要什么。


#Create list of fruits
fruits = ['banana' , 'apple' , 'orange' , 'kiwi' , 'pineapple', 'peach', 'papaya']

#Welcome user and explain fruit finder
welcome = "Welcome to the fruit finder program, please enter your name: "
words = [word.lower() for word in input('Please enter a sentence with fruit in it to begin: ').split(" ")]

#Check if input matches fruit list and display the number of fruits to user
counter = 0
for word in words:
if word in fruits:
counter += 1
print(f"{word} fruit found in your sentence")

print(f"A total of {counter} fruits found in your sentence")


First, you're checking if the entire input sentence is in the fruits list using sentence.lower() in fruits. This condition checks if the entire sentence, including spaces and capitalization, is present in the list.

首先,使用水果中的sentent.lower()检查整个输入句子是否在水果列表中。此条件检查整个句子(包括空格和大写)是否存在于列表中。


To find individual fruits within the sentence, you need to split the sentence into words and then check if each word (in lowercase) is in the fruits list. Here is how you can do this.

要在句子中查找单个水果,您需要将句子拆分为单词,然后检查每个单词(小写)是否在水果列表中。以下是如何做到这一点。


fruits = ['banana', 'apple', 'orange', 'kiwi', 'pineapple', 'peach', 'papaya']

welcome = "Welcome to the fruit finder program, please enter your name: "
sentence = input('Please enter a sentence with fruit in it to begin: ')

sentence_words = sentence.lower().split()

found_fruits = []

for word in sentence_words:
if word in fruits:
found_fruits.append(word)

if found_fruits:
print("Here are the fruits found in your sentence:", ", ".join(found_fruits))
else:
print("No fruits found in the sentence.")


Your current example will look for the entire sentence banana kiwi pineapple, which isn't in the fruits list.

您当前的示例将查找整个句子香蕉猕猴桃菠萝,它不在水果列表中。


You need to search per word. You can do this using split and a for loop.

你需要按单词搜索。您可以使用split和for循环来完成此操作。


for word in sentence.split():
if word.lower() in fruits:
print(f"{word} is a fruit")


As your list of fruits is lowercase you should also normalise the user input to the same case.

由于你的水果列表是小写的,你也应该将用户输入标准化为相同的大小写。


Having said that, a set would be more appropriate.

话虽如此,一套会更合适。


Then split the input sentence into individual words.

然后把输入的句子分成单独的单词。


Use the built-in filter() function to identify the words in the sentence you're interested in - i.e., the fruits.

使用内置的filter()函数来识别你感兴趣的句子中的单词,即水果。


Something like this:

类似于以下内容:


fruits = {'banana', 'apple', 'orange', 'kiwi', 'pineapple', 'peach', 'papaya', 'tomato'}

sentence = input('Please enter a sentence containing the name of a fruit: ')

if found := list(filter(lambda word: word in fruits, sentence.lower().split())):
print('I found the following fruit(s):', *found)
else:
print('No fruits found')


You need to split the input into a list of word to check with fruit list. You can use intersection set method to get the same value of the lists. Also, use while loop to re-ask the user for input if intersection method return none or empty:

您需要将输入拆分为单词列表,以便与水果列表进行核对。您可以使用交集集方法来获得相同的列表值。此外,如果交集方法返回none或为空,则使用while循环重新询问用户输入:


fruits = ['banana' , 'apple' , 'orange' , 'kiwi' , 'pineapple', 'peach', 'papaya']

sentence = input('Please enter a sentence with fruit in it to begin: ')

while True:
if found := set(fruits).intersection(sentence.lower().split()):
print(f"Here are the fruits found in your sentence:", *found, sep='\n')
break
else:
sentence = input('Fruit not found in sentence. Please try again: ')

# Please enter a sentence with fruit in it to begin: Strawberry Grape Melon
# Fruit not found in sentence. Please try again: Banana Kiwi Pineapple Grape Melon
# Here are the fruits found in your sentence:
# kiwi
# pineapple
# banana

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com