Creating a tumbling windows in python

后端 未结 3 1112
离开以前
离开以前 2021-01-27 20:45

Just wondering if there is a way to construct a tumbling window in python. So for example if I have list/ndarray , listA = [3,2,5,9,4,6,3,8,7,9]. Then how could I f

3条回答
  •  猫巷女王i
    2021-01-27 21:02

    If you want a one-liner, you can use list comprehension:

    listA = [3,2,5,9,4,6,3,8,7,9]
    listB=[max(listA[i:i+3]) for i in range(0,len(listA),3)]
    print (listB)
    

    it returns:

    [5, 9, 8, 9]
    

    Of course the codes can be written more dynamically: if you want a different window size, just change 3 to any integer.

提交回复
热议问题