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

时间:2017-02-05 17:58:38

标签: 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

我想使用for循环查找str2中的str1来打印该单词是否找不到,如果找到了,则索引位置(s )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
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

如果有人可以帮助我和其他任何尝试与此类似的东西的人会非常有帮助!

5 个答案:

答案 0 :(得分:2)

使用enumerate python内置函数:

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

docs:https://docs.python.org/3.3/library/functions.html#enumerate

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

检查相关文档:https://docs.python.org/3.6/tutorial/datastructures.html#more-on-lists

答案 1 :(得分:2)

你可以使用条件列表理解(就像for - 循环):

>>> str1 = 'i like to code in python and code things'
>>> str2 = 'code'

>>> indices = [idx for idx, word in enumerate(str1Input.split(), 1) if word == str2]
>>> indices
[4, 8]

给你比赛的指数。那么你可以随心所欲地做任何事情:

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

你的尝试实际上并没有太糟糕,但你犯了一个错误:for str2 in str1:这会覆盖你的str2变量,该变量包含要查找的字符串!此外index将始终为您提供变量的第一个索引,因此当您执行str1.index(str2)+1时,您会查找当前调查项的第一个出现!这就是为什么你有两次That word was found at index position: 4因为它只查找第一个'code'

文档总是有用的:

list.index

  

返回值为x的第一个项目列表中的索引。如果没有这样的项目,则会出错。

答案 2 :(得分:0)

您的问题出在for循环中:您每次迭代都会将str1的值分配给本地变量str1。它被称为变量阴影。 解决方案是在循环声明中使用不同的变量名。

答案 3 :(得分:0)

您可以做的是从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

答案 4 :(得分:0)

确保在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