Python 使用for循环查找句子中的单词及其索引位置

Python 使用for循环查找句子中的单词及其索引位置,python,string,python-3.x,for-loop,indexing,Python,String,Python 3.x,For Loop,Indexing,我正在编写一个代码,提示用户输入一个句子,然后定义为str1,然后提示用户输入一个定义为str2的单词 例如: Please enter a sentence: i like to code in python and code things Thank you, you entered: i like to code in python and code things Please enter a word: code Please enter a sentence: i like to

我正在编写一个代码,提示用户输入一个句子,然后定义为
str1
,然后提示用户输入一个定义为
str2
的单词

例如:

Please enter a sentence: i like to code in python and code things
Thank you, you entered:  i like to code in python and code things
Please enter a word: code
Please enter a sentence: i like to code in python and code things
Please enter a word: code
Thank you, you entered:  i like to code in python and code things


That word was found at index position: 1
That word was found at index position: 2
That word was found at index position: 3
That word was found at index position: 4
That word was found at index position: 5
That word was found at index position: 6
That word was found at index position: 7
That word was found at index position: 4
That word was found at index position: 9
我想使用
for
循环在
str1
中查找
str2
,以打印是否找到该单词,如果已找到,则打印
str2
的索引位置

目前我有以下代码:

str1Input = input("Please enter a sentence: ")
print("Thank you, you entered: ",str1Input)

str1 = str1Input.split()

str2 = input("Please enter a word: ")

if str2 in str1:
    for str2 in str1:
        print("That word was found at index position", str1.index(str2)+1)
else:
    print("Sorry that word was not found")
虽然,结果似乎打印出是否找到索引位置,但随后打印出句子中每个单词的索引位置。另外,如果我搜索某个单词在该句子中出现两次,它只会打印该单词在该句子中第一次出现时的索引位置,例如:

Please enter a sentence: i like to code in python and code things
Thank you, you entered:  i like to code in python and code things
Please enter a word: code
Please enter a sentence: i like to code in python and code things
Please enter a word: code
Thank you, you entered:  i like to code in python and code things


That word was found at index position: 1
That word was found at index position: 2
That word was found at index position: 3
That word was found at index position: 4
That word was found at index position: 5
That word was found at index position: 6
That word was found at index position: 7
That word was found at index position: 4
That word was found at index position: 9

如果有人能帮助我和其他人尝试类似的事情,那将是非常有帮助的

使用
枚举
python内置函数:

for index, word in enumerate(splitted_sentence):
   if word == target_word:
       print("Index: ", index)
文件:

UPD:
list.index()
方法返回匹配元素的最低索引。这就是为什么如果你的单词在一个句子中出现两次,你总是得到相同的索引


同时检查此文档:

使用
enumerate
python内置函数:

for index, word in enumerate(splitted_sentence):
   if word == target_word:
       print("Index: ", index)
文件:

UPD:
list.index()
方法返回匹配元素的最低索引。这就是为什么如果你的单词在一个句子中出现两次,你总是得到相同的索引


同时检查此文档:

您可以使用条件列表理解(它类似于
for
-loop):

给你匹配的索引。然后你可以做任何你喜欢的事情:

if indices:
    for idx in indices:
        print('word found at position {}'.format(idx)
else:
    print('word not found')

您的尝试实际上还不错,但您犯了一个错误:
用于str1中的str2:
这将覆盖保存要查找的字符串的
str2
变量!另外,
index
将始终为您提供变量的第一个索引,因此当您执行
str1.index(str2)+1时,您会查找当前调查项目的第一个出现!这就是为什么
这个词在索引位置被找到了两次:4
,因为它只查找第一个
“code”

阅读文档总是很有用的:

:

返回值为x的第一项列表中的索引。如果没有此类项目,则为错误


您可以使用条件列表理解(类似于
for
-循环):

给你匹配的索引。然后你可以做任何你喜欢的事情:

if indices:
    for idx in indices:
        print('word found at position {}'.format(idx)
else:
    print('word not found')

您的尝试实际上还不错,但您犯了一个错误:
用于str1中的str2:
这将覆盖保存要查找的字符串的
str2
变量!另外,
index
将始终为您提供变量的第一个索引,因此当您执行
str1.index(str2)+1时,您会查找当前调查项目的第一个出现!这就是为什么
这个词在索引位置被找到了两次:4
,因为它只查找第一个
“code”

阅读文档总是很有用的:

:

返回值为x的第一项列表中的索引。如果没有此类项目,则为错误


您的问题在
for
循环中:您将
str1
中的值分配给局部变量
str1
。这叫做可变阴影。
解决方案是在循环声明中使用不同的变量名。

您的问题在于
for
循环:您在每次迭代中将
str1
中的值分配给局部变量
str1
。这叫做可变阴影。
解决方案是在循环声明中使用不同的变量名。

您可以做的是从str1中列出一个列表,并找到str2在列表中出现的位置。代码如下:

   str1 = "i like to code in python and code things"
   str2 = "code"

  the_indexes = []
  the_new_list = [str1.split(' ')]


  the_count = str1.count(str2)
  if the_count > 0:



  for i in range(len(the_new_list[0])):
       if the_new_list[0][i] == str2:
           the_indexes.append(i)

  else:
      print str2, " not found in the first sentence"

  print "The indexes are: "

  for i in the_indexes:
       print i

你能做的就是从str1中列出一个列表,并找出str2在列表中的位置。代码如下:

   str1 = "i like to code in python and code things"
   str2 = "code"

  the_indexes = []
  the_new_list = [str1.split(' ')]


  the_count = str1.count(str2)
  if the_count > 0:



  for i in range(len(the_new_list[0])):
       if the_new_list[0][i] == str2:
           the_indexes.append(i)

  else:
      print str2, " not found in the first sentence"

  print "The indexes are: "

  for i in the_indexes:
       print i

只需确保在if/else语句中使用比较,并考虑循环在做什么

希望这足够简单但有效:

编辑:添加计数器,而不是使用“.index()”。只是保持它的美观和基本:

str1In = "I like to code a few things and code a lot"
str2 = "code"
str1 = str1In.split()

indexCount = 0
for word in str1:
    if word == str2:
       print("Your word was found at index point",int(indexCount))
    else:
       print("Your word was not found at",int(indexCount))
    indexCount += 1

只需确保在if/else语句中使用比较,并考虑循环在做什么

希望这足够简单但有效:

编辑:添加计数器,而不是使用“.index()”。只是保持它的美观和基本:

str1In = "I like to code a few things and code a lot"
str2 = "code"
str1 = str1In.split()

indexCount = 0
for word in str1:
    if word == str2:
       print("Your word was found at index point",int(indexCount))
    else:
       print("Your word was not found at",int(indexCount))
    indexCount += 1

你建议我如何解决“for-loop”和“index”函数,最好不改变我的方法?@LukeP_8你能澄清一下“不改变我的方法”是什么意思吗?我的意思是,通过保持循环等方式基本上保持代码的相似性。我不想对代码做太多的更改来纠正它:)我认为在这种情况下更好,或者您需要使用
list.index
?您建议我如何解决“for loop”和“index”函数,最好不改变我的方法?@LukeP_8你能澄清一下“不改变我的方法”是什么意思吗?我的意思是通过保持循环等基本上保持我的代码相似。我不想改变太多代码来纠正它:)我想在这种情况下会更好,或者您需要使用
list.index
?这很好,也很有效,尽管我的代码在单词第一次出现时仍会打印其索引位置。我有没有办法用类似的方法来解决这个问题,因为你最初的回答非常简单而且有用:)我的坏家伙。我的草率编码!。请参阅对文章的编辑,为索引点添加计数器。(每次循环重新开始时,它只会给计数加1。为这个错误道歉!非常好用!谢谢伙计!不用担心,伙计,很高兴我能帮上忙。保持简洁!这很好,很有效,尽管我的代码在单词第一次出现时仍然打印出它的索引位置。)