简体   繁体   English

在 Python 中查找列表元素的索引

[英]Find the index of a list element in Python

How could I find a certain index as to where one of the elements is located at within sets.我怎么能找到某个索引来确定其中一个元素在集合中的位置。 So I would like to find where (2.0, 2.0, 152) is located within the sets list, which is the 5th index.所以我想找到(2.0, 2.0, 152)sets列表中的位置,这是第 5 个索引。 Is there a function that I would be able to use to get to that?有没有我可以用来实现的 function ?

from itertools import groupby
from itertools import accumulate
import itertools

#Configs
Lower = [round(x * 0.5,1) for x in range(4,23)]   #ideal = (4,13) Lower and upper boundary values 
Upper = [round(x * 0.5,1) for x in range(4,23)] # ideal between (10-23)
number = [round(x *0.5,5) for i in range(100, )]    #used for calculations with stand deriv and linear reg
list_set = [Lower, Upper, number]
sets = []
for element in itertools.product(*list_set):
    print(element)
    sets.append(element)

You can use the index -method for that:您可以为此使用index方法:

sets.index((2.0, 2.0, 152))

The method raises a ValueError if the supplied value is not in the list, which seems to be the case for (2.0, 2.0, 152) .如果提供的值不在列表中,则该方法会引发ValueError ,这似乎是(2.0, 2.0, 152)的情况。

There are two ways you could go about finding the index of an element from an array in Python.您可以通过两种方法 go 从 Python 中的数组中查找元素的索引。

The first and the most straightforward way to do so is by using the .index() method of any list, which returns the index at which the specified value is located.第一种也是最直接的方法是使用任何列表的.index()方法,该方法返回指定值所在的索引。 Take this example:举个例子:

list = [123, 417, 098]
print(list.index(417))

You can see here that it will print one, because that is the index at which 417 is located in the list array.您可以在此处看到它将打印一个,因为这是417list数组中所在的索引。 Though, keep in mind that you might get an error if the specified value doesn't exist.但是,请记住,如果指定的值不存在,您可能会收到错误消息。

The second and harder way is by iterating through the array yourself.第二种更难的方法是自己遍历数组。 Take this example:举个例子:

list = [123, 417, 098]

def findElement(array, element):
    for index in range(len(array)):
        if (array[index] == element):
            return index

print(findElement(list, 417))

What we did here was to define a method which takes as parameters an array and an element, then, iterates through every index of the list, until the value at that index is the specified element.我们在这里所做的是定义一个方法,该方法将数组和元素作为参数,然后遍历列表的每个索引,直到该索引处的值是指定的元素。 When the values match, it will return the said index.当值匹配时,它将返回所述索引。 If no values will ever match, it won't return anything.如果没有任何值会匹配,它不会返回任何东西。

The advantage of the second approach is the fact that it won't throw an error if there is no such element in the list, however, it is also a bit harder to set up.第二种方法的优点是,如果列表中没有这样的元素,它不会抛出错误,但是,它也有点难以设置。

try:
    print(sets.index(element))
except:
    print('not found')

If you gonna use more better keep in a dict indexes,which can retrieve you index in O(1) time.Construction can take O(n) time though如果你要使用更好的字典索引,它可以在 O(1) 时间内检索你的索引。虽然构造可能需要 O(n) 时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM