gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-24 17:54:46 34 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

我是一名学习蟒蛇的学生,目前正在为我的班级做一项作业。我在尝试使用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.

您当前的示例将查找完整的句子banana kiwi pineApple,它不在水果列表中。

优秀答案推荐

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.

首先,在水果中使用senence.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.

您当前的示例将查找完整的句子banana kiwi pineApple,它不在水果列表中。


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或Empty,则使用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

更多回答

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