Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 根据关键字查找并显示列表中的所有索引_List_Indexing_Python 3.4 - Fatal编程技术网

List 根据关键字查找并显示列表中的所有索引

List 根据关键字查找并显示列表中的所有索引,list,indexing,python-3.4,List,Indexing,Python 3.4,我在Python3中有以下代码,用于查找和显示列表中搜索到的单词的第一个示例,即单词“now”位于位置3,但是如果该单词在列表中出现两次或两次以上,我无法找到显示第二个/第三个etc索引的方法。i、 e.“现在”一词出现在位置3和5 我在网上看到过一些非常复杂的版本,但我想看看我是否能让它变得非常简单。我知道。index()只找到第一个索引,所以还有其他方法可以使用吗 代码: 非常感谢试试看 positions=[ind代表ind,如果x==word\u to\u find,则枚举(单词)中的x

我在Python3中有以下代码,用于查找和显示列表中搜索到的单词的第一个示例,即单词“now”位于位置3,但是如果该单词在列表中出现两次或两次以上,我无法找到显示第二个/第三个etc索引的方法。i、 e.“现在”一词出现在位置3和5

我在网上看到过一些非常复杂的版本,但我想看看我是否能让它变得非常简单。我知道。index()只找到第一个索引,所以还有其他方法可以使用吗

代码:

非常感谢

试试看

positions=[ind代表ind,如果x==word\u to\u find,则枚举(单词)中的x]


现在
positions
将列出
word\u要在
words
中查找的所有索引,这太棒了。非常感谢Praveen。我将研究这句话的真正含义。
sentence=input("Enter your sentence here: ")
sentence=sentence.lower()
words=(sentence.split())
print(words)
word_to_find=input("What word position do you want to find? ")
word_to_find=word_to_find.lower()

if word_to_find in words:
    position=words.index(word_to_find)
    print( "The word ", word_to_find, "is in position(s): ",position+1)
else:
    print("No word exists")