python - Counting items in list before consecutive instance -
i'd count amount of items in array before consecutive amount of zeroes inputted user.
['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'e']
for example, if user inputs 3, there 3 items in list before consecutive amount of 3 zeroes.
at moment code checks amount of consecutive zeroes , returns based on - how can change gain value of items before consecutive instance?
largeblock = max(sum(1 _ in g) k, g in groupby(line) if k == '0')
seats, wanted = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'e'], 3 itertools import groupby occ, grp in groupby(enumerate(seats[:-1], 1), key = lambda x: x[1]): if occ == '0': available = list(grp) if len(available) >= wanted: print([seats[-1] + str(item[0]) item in available[:wanted]]) # ['e4', 'e5', 'e6']
Comments
Post a Comment