简体   繁体   English

Python:找到第一个x的索引,然后找到第二个x的索引

[英]Python: Find index of the first occurrence of x and then the index of the second occurrence of x

I am trying to loop through a list and find the index of the first occurrence of x and the index of the second occurrence of x. 我试图遍历一个列表,找到x的第一次出现的索引和x的第二次出现的索引。

I have the following list: 我有以下清单:

int_list = [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 14, 15, 15, 17, 17, 14, 20, 20, 5, 23, 23, 25, 26, 26, 28, 28, 25, 31, 32, 32, 31, 35, 35, 4, 3, 39, 39, 41, 42, 42, 44, 44, 41, 47, 47, 2, 50, 50, 1, 53, 54, 55, 56, 57, 58, 58, 60, 60, 62, 62, 57, 65, 66, 67, 67, 66, 65, 56, 72, 73, 74, 75, 75, 74, 78, 78, 73, 72, 82, 83, 83, 85, 85, 82, 55, 89, 90, 91, 92, 93, 93, 92, 91, 90, 98, 99, 99, 98, 102, 103, 103, 102, 106, 106, 89, 109, 109, 54, 112, 113, 113, 112, 116, 116, 53, 119, 120, 121, 121, 120, 119, 0, 126, 127, 128, 128, 130, 131, 131, 130, 127, 135, 135, 126]

I would like to start from index 0 which is a 1, as this is the first occurrence assign this to first_index, then search the rest of the list until I find another 1 which is at index 1 and so on. 我想从索引0(它是1)开始,因为这是第一次出现,将其分配给first_index,然后搜索列表的其余部分,直到找到索引1处的另一个1,依此类推。

I understand I need to use enumerate() which will loop through my list and allow me to find the index, however I am struggling with how to find the first and second occurrences 我知道我需要使用enumerate(),它会遍历我的列表并允许我找到索引,但是我在寻找如何找到第一个和第二个匹配项方面很费劲

for example: 例如:

for i, e in enumerate(int_list):
    # if e 'is the first occurrence':
         first_index = i
    # else e is second occurrence:
         second_index = i

I don't know how to determine whether e is the first or second occurrence 我不知道如何确定e是第一次出现还是第二次出现

I have tried the following code: 我尝试了以下代码:

for i in int_list:

    first_ind = firstInt_ls.index(i, 0)
    second_ind = firstInt_ls.index(i, first_ind+1)

    print(first_ind)
    print(second_ind)

Which works unless there is only one occurrence. 除非只有一次出现,否则该方法有效。 If there is not a second occurrence it produces a ValueError: 10 is not in list 如果没有第二次出现,则会产生ValueError:列表中没有10

Use list's index() function, where the first argument is the element you are trying to find and the second argument is the index to start from. 使用列表的index()函数,其中第一个参数是您要查找的元素,第二个参数是要从其开始的索引。 Maybe something like this: 也许是这样的:

a = [1, 2, 3, 1, 4, 5, 6, 1, 5]

first_ind_of_one = a.index(1, 0)
second_ind_of_one = a.index(1, first_ind_of_one+1)

print(first_ind_of_one)
print(second_ind_of_one)

Code below for case when we don't know if and how many occurrences of element in list: 下面的代码用于当我们不知道列表中元素是否出现以及出现多少时的情况:

occurrences_needed = 2
list_of_inds = []
for ind, elem in enumerate(a):
    if len(list_of_inds) == occurrences_needed:
        break
    if elem == 1:
        list_of_inds.append(ind)

print(list_of_inds)

Just for fun, I have added a third method that is easier to read but may be inefficient for large lists (some testing may be required): 只是为了好玩,我添加了第三种方法,该方法更易于阅读,但对于大型列表而言可能效率不高(可能需要进行一些测试):

s = ''.join(str(i) for i in a)
first_ind = s.find('1')
second_ind = s.find('1', first_ind + 1)
print(first_ind)
print(second_ind)

This converts the list to a string and use's string's find() method. 这会将列表转换为字符串,并使用字符串的find()方法。 This method simply returns -1 in the event where the element is not found. 如果找不到元素,则此方法仅返回-1。 So you may have to check for that in your code. 因此,您可能必须在代码中进行检查。

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

相关问题 在 numpy 数组中查找第一次出现不是 X 或 Y 的某个值的索引 - Find the index of the first occurrence of some value that is not X or Y in a numpy array Python在索引后找到第一个出现的字符 - Python find first occurrence of character after index 在DataFrame中查找第一次出现的索引 - Find the index of first occurrence in DataFrame 在 Python 中找到 substring 的第二次出现(索引)而不重叠 - Find the second occurrence (index) of substring without overlapping in Python 如何在 Python 3 中查找字符串中第二次出现的短语的索引? - How to find index of second occurrence of a phrase in a string in Python 3? 如何将字符串从第一次出现的子字符串的索引切片到Python中第二次出现的子字符串? - How can I slice a string from the index of the first occurrence of a sub string to the second occurrence of a sub string in Python? 查找列表中字符串第二次出现的索引 - Find the index of the second occurrence of a string inside a list Python - 查找字符串中第一次出现的字符串列表的索引位置 - Python - find index position of first occurrence of a list of strings within a string 在排序列表中查找首次出现的索引 - Find index of first occurrence in sorted list 在索引之前查找子字符串的第一次出现 - Find First Occurrence of Substring Before Index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM