am trying to fit in None's if there is any index missing in the list.
mylist = [1,2,3,4,5,8,9]
In the above list, i wanna fit in none's at 6,7 and 10
end result = [1,2,3,4,5,None,None,8,9,None]
I wont be having any missing value until element 5. But after that, i may miss all elements or few. Am trying to make my list length 10 by inserting None's in the missed elements. few sample missing elements :
1 - [1,2,3,4,5,6] , expected result - [1,2,3,4,5,6, None, None, None, None]
2 - [11,13,12,4,5,7,9 ], expected result - [11,13,12,4,5,None,7,None,9,None]
Logic - I have used the distance -
distance = (-1 * (mylist[3]-mylist[4]))
index_missing = (-1*(mylist[3] -mylist[5])) which is equal to 2 (i.e for the above sample 1 )
Code:
if len(mylist) < 7:
if index_missing == 2:
mylist[7:1] = [None,None, None, None]
elif len(mylist) < 8:
do something
elif len(mylist) < 9:
.....
Suppose if its 3 then do another if else. Like this I will have to fit None's based on how many values are missing. I had to right so many if cases which is making my code complicated. Need some help on this. Can we use something like while loop.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire