lundi 7 septembre 2020

How to make a function that adds a sublist to a list

I want to create a NewList of values (between 1 and 0) using a function that adds a sublist of values (low_phase=[0] x 30) to a second list (high_phase=[1] x 960 elements long). I need a loop because the function needs to go through all elements in time, and I need an if function, that checks when the elements in time are equal to the ones of the list interval, and only then apply the sublist low_phase. When the values are equal the list should contain 0, when the values are different, then 1.

#this is the `NewList` I want to create. It has to contain values of 1 or 0, which are assigned based on the function with loop 

NewList = []

#this is the first list 
time = list(range(1,960))
#this is the second list 
interval= list(range(60,960,60))

#these are the values to be assigned to the newLIst if the condition is true
high_phase = [1]*960
#these are the values to be assigned to the newLIst if the condition is False
low_phase = [0]*29


def method(NewList):
    for n,m in zip(time,interval):
        if time[n] == interval[m]:
            NewList.extend(low_phase)
        else:
            NewList.append(high_phase)
print(NewList)

Aucun commentaire:

Enregistrer un commentaire