Last active
January 17, 2022 05:34
-
-
Save chuongmep/29a736a21ee77afbc7796e5896252817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import groupby, count | |
bol = [None, "2018",None,None,None,"2018","2019","2020",None,None] | |
value = "Data" | |
def get_bol(a): | |
index = [] | |
for i in range(len(a)-1): | |
if isinstance(a[i], str): | |
index.append(i) | |
return index | |
def set_bol(a,intervals,value): | |
for i in intervals: | |
a[i] = value | |
return a | |
def intervals(data): | |
out = [] | |
counter = count() | |
for key, group in groupby(data, key = lambda x: x-next(counter)): | |
block = list(group) | |
out.append(block[-1]+1) | |
return out | |
id = intervals(get_bol(bol)) | |
print(set_bol(bol,id,value)) | |
#Output | |
# [None, '2018', 'Data', None, None, '2018', '2019', '2020', 'Data', None] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem Resolve