Reputation: 63
I have a lists of integers like mylist = [40,46,46,50,50,54,54,67,75,75,79,79,87...
The lists are strictly increasing, where if a number repeats it only repeats one time (like 46
in ..40,46,46,50..
) and differences between continues repeats are always the same ( like for mylist
its 4 ..40,46,46,50,50,54,54..
because 50-46 = 4 , 54-50 = 4 and so on).
I would like to find out longest streak of this doubling pattern. For example for this part of mylist
there are two streaks (46,46,50,50,54,54
) where the repeat streak is 3 and (75,75,79,79
) where the repeat streak is 2 .
I have different lists where the differences can change (can be 4 or 5 or 6) but this doubling and increasing pattern don't change.
How can I find longest repeating streak (which is 3 for mylist
example) ? I couldn't find a concise and simple solution without using a lot of for loops and if conditions.
Upvotes: 1
Views: 656
Reputation: 450
You can do it as follows:
from itertools import groupby
aux = [sum(1 for _ in b) for a,b in groupby(mylist)]
aux2 = [sum(1 for _ in b if a != 1) for a,b in groupby(aux)]
consecutive = max(aux2)
print(consecutive)
Output
3
Where consecutive is the value you are looking for. By repeating the same operation twice, it first transforms your list into a list of integers, where each repeated value is now represented as a 2:
print(aux)
[1,2,2,2,1,1,1,...]
It then does the exact same thing if the value is not a 1, so the second list is now:
print(aux2)
[1,3,...]
And then you return the max value of that list.
Upvotes: 0
Reputation: 61910
You could use groupby twice and then max with len as the key
:
from itertools import groupby
from operator import itemgetter
mylist = [40, 46, 46, 50, 50, 54, 54, 67, 75, 75, 79, 79, 87]
groups = ((k, sum(1 for _ in v)) for k, v in groupby(mylist))
streaks = [list(v) for k, v in groupby(groups, key=itemgetter(1)) if k == 2]
result = max(streaks, key=len)
print(result)
Output
[(46, 2), (50, 2), (54, 2)]
The first iteration, groups the numbers in consecutive equals values, the second one does it by streak size, finally max just retrieve the largest of those groups.
If you wish to unpack the result, just do:
print([n for n, count in result for _ in range(count)])
Output
[46, 46, 50, 50, 54, 54]
Upvotes: 1