[Python] I want to write a simple python code for sliding window (3) of a list of numbers...

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying the below python code

    lst = [1,2,3,4,5,6,7,8]
    lst2=[]
    def sliding_window(elements, window_size):
    for i in range(len(elements)):
    a= lst+lst[i+1]+lst[i+2]+lst[i+3]+lst[i+4]
    a=a/3
    lst2.append(a)

    if window_size<len(lst2):
    break



    sliding_window(lst, 3)

    print(lst2)


    The output i was expecting is [2,3,4,5,6,7]

    Continue reading...

Compartilhe esta Página