英文:
How do you store a list of strings in an index function?
问题
sentence = input("输入句子:")
punctuation = [" ", ",", ".", ":", "?", "!"]
interruption1 = None
for char in sentence:
if char in punctuation:
interruption1 = sentence.index(char)
break
if interruption1 is not None:
word1 = sentence[:interruption1]
print(word1)
英文:
sentence = input("Input sentence: ")
punctuation = [" ", ",", ".", ":", "?", "!"]
interruption1 = sentence.index(punctuation)
word1 = sentence[:interruption1]
print(word1)
In this question, the main aim is to have the program print the first word that the user types by identifying a character that implies the first word has ended (the punctuation characters in the 'punctuation' variable). I want the program to accept the 'punctuation' variable within the index function but it sends an error message saying "must be str, not list". I tried for loops, they don't work here either as far as I know.
A previous question of mine gave me information that you can't use boolean values to represent a set of values in a variable, so I used a list, but now this error happens, and there is absolutely nothing on the Internet on this sort of problem (neither do I have an IT teacher or any friends that do Python), so I had to come here after about an hour of trying random combinations of code. How do I make Python accept the list and use it inside the index function? Thank you.
答案1
得分: 2
你不需要使用索引,除非需要;只需在句子中接受字符,直到遇到标点符号。所以最简单的循环方法是:
sentence = input("输入句子: ")
punctuation = [" ", ",", ".", ":", "?", "!"]
result = ""
for c in sentence:
if c in punctuation:
break
else:
result = result + c
print(result)
英文:
You don't need to use an index unless it is demanded; just keep accepting characters from the sentence until you come to a punctuation. So the simplest looping approach is:
sentence = input("Input sentence: ")
punctuation = [" ", ",", ".", ":", "?", "!"]
result = ""
for c in sentence:
if c in punctuation:
break
else:
result = result + c
print(result)
答案2
得分: 1
老实说,这是一件你应该使用正则表达式来完成的事情。但是,这并没有真正回答你的问题,所以 - 你已经很接近了,但你的问题在于你传递了一个字符串列表而不是单个字符串,正如你的错误消息所暗示的那样。
你应该遍历列表中的每个字符串,并获取句子中字符串的第一个出现位置。你可以使用 str.index()
,但我更喜欢使用 str.find()
,如果字符没有找到,它将返回 -1,因此我们不必处理错误异常。
sentence = input("输入句子:")
punctuation = [" ", ",", ".", ":", "?", "!"]
matches = []
for character in punctuation:
match = sentence.find(character)
# 如果找到了字符,则保存到一个单独的列表中。
# 没有匹配意味着值为-1,所以通过 >0 来忽略它。
if match > 0:
matches.append(match)
# 现在找到首次出现的匹配项:
first_match_index = min(matches)
# 返回第一个单词
first_word = sentence[:first_match_index]
print(first_word)
英文:
To be honest this is something you should do with regular expressions. But, that doesn't really answer your question, so - you are close, but your problem is that you're passing a list of strings instead of a single string, as your error message implies.
You should loop through each string in the list and get the first occurrence of the string in your sentence. You can use str.index()
but I prefer to use str.find()
which will return a -1 if the character is not found thus we don't have to mess around with error exceptions.
sentence = input("Input sentence: ")
punctuation = [" ", ",", ".", ":", "?", "!"]
matches = []
for character in punctuation:
match = sentence.find(character)
#If the character is found, save to a separate list.
#No match means value is -1, so ignore it using >0
if match >0:
matches.append(match)
# Now find the match that occurs first:
first_match_index = min(matches)
# Return first word
first_word = sentence[:first_match_index]
print(first_word)
答案3
得分: 0
如何使Python接受列表并在索引函数内使用它?
你不能,str.index 只接受字符串。
我尝试了for循环,它们不起作用
for循环可以工作
- 在保持索引的同时遍历
sentence
- 对于每个字符,检查是否在
punctuation
中- 如果不在
punctuation
中- 继续下一个字符
- 如果在
punctuation
中- 使用索引来提取第一个单词,使用切片:
sentance[:index]
- 停止迭代
- 使用索引来提取第一个单词,使用切片:
- 如果不在
英文:
> How do I make Python accept the list and use it inside the index function?
You can't str.index only accepts str.
> I tried for loops, they don't work
A for loop could work
-
iterate over
sentence
while keeping track of the index -
for each character check to see if it is in
punctuation
- if it isn't in
punctuation
- continue with the next character
- if it is in
punctuation
- use the index to extract the first word using a slice:
sentance[:index]
- stop iterating
- use the index to extract the first word using a slice:
- if it isn't in
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论