Fill in range between two lists

Gergely Szarka :

I have a set of lists like so:

listoflists = ['list1','list2']
list1_start = pd.DataFrame([1,6,11,24,33,45,55])
list1_end = pd.DataFrame([2,8,15,26,37,48,90])

list2_start = pd.DataFrame([5,9,44,72])
list2_end = pd.DataFrame([7,17,52,80])

I would like to create a new list of numbers for inbetween the start and end lists:

expected outcome:

list1_duration = [1,1.5,2,6,6.5,7,7.5,8,11,11.5,12,12.5,13,13.5,14,14.5,15....
list2_dutation = [5.5,6,6.5,7,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.6,17...

I tried to get the difference between the two and then create a range for those differences that I can add back to the starting list, but all i managed to get were a list of ranges, also that is just for one list and I don't know how to iterate through all the lists from list of lists (tried, but failed).

list1_dif = (list1_end)-(list1_start)

list1_dif = list1_dif[0].tolist()

list1_dur = []

for i in range(len(list1_dif)):
    command = ''
    command = 'list1_dur.append(list(range('+str(list1_dif[i])+')))'
    exec(command)

list1_duration = list1_dur + list1_start

print(list1_dur)

output:

[[0], [0, 1], [0, 1, 2, 3], [0, 1], [0, 1, 2, 3], [0, 1, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]]

Can anyone help me out on this?

Thanks,

Gergely

jammin0921 :

has .arange() that will get the values between and then you can use the .extend() on a list instead of .append() Try the code snippet below:

import numpy as np
list1_start = [1,6,11,24,33,45,55]
list1_end = [2,8,15,26,37,48,90]

list2_start = [5,9,44,72]
list2_end = [7,17,52,80]

def fill_in_range(list_start,list_end):
    if len(list_start) == len(list_end):
        new_list = []
        for x,y in zip(list_start, list_end):
            new_list.extend(np.arange(x,y+0.0000001,0.5))
        return new_list
    else:
        return 'Lists are not the same size'

list1_duration = fill_in_range(list1_start, list1_end)
list2_duration = fill_in_range(list2_start, list2_end)

list1_duration & list2_duration variables:

list1_duration = [1.0, 1.5, 2.0, 6.0, 6.5, 7.0, 7.5, 8.0, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0...]

list2_duration = [5.0, 5.5, 6.0, 6.5, 7.0, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 44.0, 44.5, 45.0, 45.5, 46.0...]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379581&siteId=1