user5368737 user5368737 - 1 year ago 142
Python Question

Get start and end indices of longest streak of repetitions in a list

I'm putting together something where I want to show the longest streak of wins of a team and the start and end date of this particular streak. So for example, if I have the following two lists:

streak = ["W", "W", "W", "L","W", "W", "W", "W", "W", "L"]
dates = ["2016-06-15", "2016-06-14", "2016-06-13", "2016-06-10", "2016-06-09", "2016-06-08", "2016-06-05", "2016-06-03", "2016-06-02", "2016-06-02"]

Then if I wanted to get the longest streak I could do something like:

from itertools import groupby
longest = sorted([list(y) for x, y in groupby(streak)], key = len)[-1]
print longest
["W", "W", "W", "W", "W"]

Now my idea was (let me know if this can be done better) to somehow get the start and end indices of this longest streak, so in this case:

start, end = get_indices(streak, longest) # 8, 4
print "The longest streak of {}{} was from {} to {}.".format(len(longest), longest[0], dates[start], dates[end])
"The longest streak of 5W was from 2016-06-02 to 2016-06-09.

How can I do this? Or is there a better way to do this, for example zipping the lists together and doing something with that?

Answer Source

Taking into account your code, you can still go ahead with itertools and use the underdog takewhile:

from itertools import takewhile, groupby
import itertools

L = [list(y) for x, y in groupby(streak)]
l = sorted(L, key=len)[-1]

ix = len(list(itertools.chain.from_iterable(takewhile(lambda x: x!=l, L))))

print("the longest streak goes from " + dates[ix+len(l)] + " to " + dates[ix])
#the longest streak goes from 2016-06-02 to 2016-06-09
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download