Search code examples
pythonloopsif-statementsublist

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)

example of output: for every time that interval (60, 120, 180 etc) is contained in time (0-960), add a list of 30 values = 0 to NewList, otherwise add 1 to NewList.
=================

     time   interval    NewList
     ...                    1
      58                    1
      59                    1
      60        60          0
      61                    0
      62                    0
     ...                    0
      90                    1
      91                    1
      92                    1
     ....                   1
     120       120          0
     121                    0
     122                    0
     ...                    0
     150                    1

Solution

  • Okay, I think now I got what you want: A list of 960 1s with ranges of 30 0s starting at each of the intervals. And that's exactly how you can do it: start with 960*[1] and replace slices of 30 elements with 30*[0] for each of the intervals.

    result = [1] * 960
    for i in range(60, 960, 60):
        result[i:i+30] = [0] * 30
    

    (Note: In case the last slice of 30 elements is longer than the list, this will extend the list with a few more 0, so you may have to truncate it back to 960 elements at the end.)

    Output (some):

    >>> list(enumerate(result))
    [...
     (58, 1),
     (59, 1),
     (60, 0),
     (61, 0),
     (62, 0),
     ...
     (88, 0),
     (89, 0),
     (90, 1),
     (91, 1),
     (92, 1),
     ...]
    

    About your original method function: There were a number of problems. First, you zipped two list with vastly different length (960 vs. 15 elements), so you will only get the first 15 elements from both. Then, n and m are already the elements from that list, but with time[n] == interval[m] you treat them as indices, which will give an IndexError as the first interval 60 is already larger than the entire list of intervals. Regardless of that, the condition can never be true since intervals start at a higher value and the numbers rise faster, thus the "paired" values can never be equal.