I have lists that contain None like the following lists.
l1 = [None, 1, None, None, 2, None, None]
l2 = [None, 1, 1, None, None, None, 2, None, None]
I want to get the first None position in this list which gives the maximum consecutive None count.
get_start_None_pos(l1) # should return 2
get_start_None_pos(l2) # should return 3
My current approach with Pandas which works fine but it too slow when I have so many lists to deal with.
def get_start_None_pos(l: list) -> int:
s = pd.Series(l)
s = s.isna()
s = s.cumsum() - s.cumsum().where(~s).ffill().fillna(0)
return int(s.idxmax() - s.max() + 1)
I would like to know, is there any better way to solve something like this?