简体   繁体   中英

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.

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.

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

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

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

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. 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. This method simply returns -1 in the event where the element is not found. So you may have to check for that in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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