python - Filling an array with missing contigous numbers and getting the index -
having list (no repeated values):
[67000, 67002, 67003, 67004, 67005, 67006, 67009]
i want fill list contigous numbers missing on it, desired output be:
[67000, 67001, 67002, 67003, 67004, 67005, 67006, 67007, 67008, 67009]
and index of items have been added list:
indx_list = [1,7,8]
this try:
lista_num = [67000, 67002, 67003, 67004, 67005, 67006, 67009] in xrange(len(lista_num)-1): if lista_num[i] != lista_num[i+1]-1: print lista_num[i] lista_n = lista_num[i] lista_nu = lista_num[i+1] while lista_n < lista_nu-1: lista_n = lista_n + 1 lista_num.insert(i+1, lista_n) else: print "ok"
but i'm getting following output, not desired output. think i'm messing indexes.
[67000, 67001, 67002, 67003, 67004, 67005, 67006, 67009]
i haven't tried part of getting index of items first step of getting contigous number list not working.
how can fix code , archieve goal? in advance.
suppose original list is
a = [67000, 67002, 67003, 67004, 67005, 67006, 67009]
so required target list can found with
out = range(min(a), max(a) + 1)
and added indices can found with
[i (i, v) in enumerate(out) if v not in a]
(the complexity of last line quadratic; reduce linear, can do
sa = set(a) [i (i, v) in enumerate(out) if v not in sa]
.)
Comments
Post a Comment