Python 在列表中的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4081672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:09:22  来源:igfitidea点击:

Position in an list?

pythonlistposition

提问by kame

I will check if a word exists in a list. How can I show the position of this word?

我将检查列表中是否存在某个单词。我怎样才能显示这个词的位置?

采纳答案by Gabi Purcaru

list = ["word1", "word2", "word3"]
try:
   print list.index("word1")
except ValueError:
   print "word1 not in list."

This piece of code will print 0, because that's the index of the first occurrence of "word1"

这段代码将打印0,因为这是第一次出现的索引"word1"

回答by Doug T.

Sounds like you want indexof. From here:

听起来你想要 indexof。从这里

operator.indexOf(a, b)? Return the index of the first of occurrence of b in a.

operator.indexOf(a, b)? 返回 a 中 b 第一次出现的索引。

回答by Antoine Pelisse

you can use ['hello', 'world'].index('world')

您可以使用 ['hello', 'world'].index('world')

回答by Josh Lee

To check ifan object is in a list, use the inoperator:

要检查,如果对象是在列表中,使用in操作:

>>> words = ['a', 'list', 'of', 'words']
>>> 'of' in words
True
>>> 'eggs' in words
False

Use the indexmethod of a list to find out wherein the list, but be prepared to handle the exception:

使用index列表的方法,找出其中在列表中,但要准备处理异常:

>>> words.index('of')
2
>>> words.index('eggs')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'eggs' is not in list

回答by anthony joshua

The following code:

以下代码:

sentence=["I","am","a","boy","i","am","a","girl"]
word="am"
if word in sentence:
    print( word, " is in the sentence")
    for i, j in enumerate(sentence):
        if j == word:
            print("'"+word+"'","is in position",i+1)

would produce this output:

会产生这个输出:

"am" is in position 1
"am" is in position 5

This is because in python the indexing starts at 0

这是因为在 python 中索引从 0 开始

Hope this helped!

希望这有帮助!

回答by Feri

Assuming the word is named for example "Monday":

假设这个词被命名为例如“星期一”:

You will need a list as your initial database:

您将需要一个列表作为初始数据库:

myList = ["Monday", "Tuesday", "Monday", "Wednesday", "Thursday", "Friday"]

Then you need to loop through your list one by one to the end using for, next(), iter() and len() function:

然后你需要使用 for、next()、iter() 和 len() 函数一个一个地循环你的列表到最后:

myIter = iter(myList)
for i in range(0, len(myList)):
next_item = next(myIter)

Now while looping, you need to check if the wanted word exists and wherever it exists, print it:

现在在循环时,您需要检查想要的单词是否存在以及它存在的地方,打印它:

if next_item == "Monday":
    print(i)

Altogether:

共:

myList = ["Monday", "Tuesday", "Monday", "Wednesday", "Thursday", "Friday"]
myIter = iter(myList)
for i in range(0, len(myList)):
    next_item = next(myIter)
    if next_item == "Monday":
        print(i)

Since there are two Mondays in this list, the result for this example will be: 0 2

由于此列表中有两个星期一,因此此示例的结果将为:0 2

回答by SRG

using enumeration - to find all occurences of given string in list

使用枚举 - 在列表中查找给定字符串的所有出现

listEx=['the','tiger','the','rabit','the','the']
print([index for index,ele in enumerate(listEx) if ele=='the'])

output

输出

[0, 2, 4, 5]