在列表中查找有限长度的最大值

moe*_*eth 4 python list

我正在寻找chunked list中的最大绝对值.

例如,列表是:

[1, 2, 4, 5, 4, 5, 6, 7, 2, 6, -9, 6, 4, 2, 7, 8]
Run Code Online (Sandbox Code Playgroud)

我想用lookahead = 4找到最大值.对于这种情况,它会返回给我:

[5, 7, 9, 8]
Run Code Online (Sandbox Code Playgroud)

我怎样才能在Python中完成?

for d in data[::4]:
    if count < LIMIT:
        count = count + 1

        if abs(d) > maximum_item:
            maximum_item = abs(d)
    else:
        max_array.append(maximum_item)

        if maximum_item > highest_line:
            highest_line = maximum_item

        maximum_item = 0
        count = 1
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用for循环来检查这个.但我确信在python中有一种更简单的方法.

eum*_*iro 5

使用标准Python:

[max(abs(x) for x in arr[i:i+4]) for i in range(0, len(arr), 4)]
Run Code Online (Sandbox Code Playgroud)

如果阵列不能均匀分割,这也适用.