-2

What is the most efficient way to find the index of consecutively repeating numbers? The index should be the index of the first number in the consecutively repeating series.

My first thought is the itertools library, but how can i find the index of the number? I tried enumerate and find one solution for this question:

def foo(ls):

    result = [None, 0, -1] # record number, number of occurrence, and index
    track = [None, 0, -1] # keep track for the current number
    for index, value in enumerate(ls):
        if value == track[0]:
            track[1] += 1
        else:
            track[0] = value
            track[2] = index

        if track[1] > result[1]:
            result[0] = track[0]
            result[1] = track[1]
            result[2] = track[2]

     return result[2]

For example, if the input is the list [1,2,3,3,3,9,9,9], now the 3 and 9 both consecutively repeated three times, the output should be the index of the bigger one (9), that is, 5. And if the input is the list [1,9,9,3,2,9,9,9] the output should be the index 5.

Chris XU
  • 13
  • 1
  • 2
  • Can you please share the code that you have tried – Jeril May 02 '19 at 04:33
  • `What is the most efficient way to find...?` - if there are no other alternatives, one that works. – wwii May 02 '19 at 04:35
  • `...itertools library, but how can i find the index of the number?` If you need an item's index while iterating use [enumerate](https://docs.python.org/3/library/functions.html#enumerate) – wwii May 02 '19 at 04:37
  • Welcome to SO. Unfortunately this isn't a discussion forum or tutorial service. Please take the time to read [ask] and the other links found on that page. – wwii May 02 '19 at 04:39
  • related: [What's the most Pythonic way to identify consecutive duplicates in a list?](https://stackoverflow.com/q/6352425/2823755) – wwii May 02 '19 at 04:44
  • You don't need to use itertools, just find the maximum value and it's index, check my answer below @ChrisXU – Devesh Kumar Singh May 02 '19 at 08:35

2 Answers2

1

Not sure if this is efficient. Using itertools.groupby

Ex:

from itertools import groupby

l1 = [1,9,9,3,2,9,9,9, 1,2]
#Group by elements --> https://stackoverflow.com/questions/6352425/whats-the-most-pythonic-way-to-identify-consecutive-duplicates-in-a-list
grouped_L = [(k, list(g)) for k,g in groupby(enumerate(l1), lambda x: x[1])]   

print( max(grouped_L, key=lambda x: (x[0], len(x[1]))) )
print( max(grouped_L, key=lambda x: (x[0], len(x[1])))[1][0][0] )  #Get Index

Output:

(9, [(5, 9), (6, 9), (7, 9)])
5
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • @DeveshKumarSingh Sorry I do not understand? overkill for `groupby`? – Rakesh May 02 '19 at 10:50
  • @DeveshKumarSingh I think you misunderstood the question OP does not need the index of the max number but the index of consecutively repeating numbers...Ex: `[1,9,9,3,2,9,9,9]` --> 5 – Rakesh May 02 '19 at 10:53
  • @DeveshKumarSingh in `[1,9,9,3,2,9,9,9]` max consecutively repeating series is `9,9,9` so return index of the first element in this which is `5` – Rakesh May 02 '19 at 11:01
  • Thanks! That is a very good solution, but if the input is [1, 9, 9, 3, 2, 8, 8, 8] the output should be the index of the first 8, i.e. 5. If in max function we compare tuple, when the first element of the first tuple is greater than that of the second tuple, then no matter how many times it consecutively repeated it would be greater, thus the result would be 1 instead of 5. – Chris XU May 02 '19 at 18:56
  • i changed the key in max function to be lambda x: len(x[1]) ** x[0] so that the number of repeating is dominant. Or simply change the key to be lambda x: (len(x[1]), x[0]). I think it should work. Thank you for this excellent answer! – Chris XU May 02 '19 at 19:03
  • Cool. I did not know you need the repeating numbers as dominant. – Rakesh May 02 '19 at 19:27
0

use the .index() method on a list and pass the value you want as a parameter and it will return the index of the first occurence :

data = [1,3,5,6,5,9,3,8,5]
ind = data.index(5)
print(ind)

the code above will return 2.

Amr Bashir
  • 103
  • 1
  • 2
  • 12