简体   繁体   English

如何在Python中创建不同大小的滑动窗口?

[英]How do I create a sliding window in Python with different sizes?

I have a list of lists. 我有一份清单清单。 For example: 例如:

a = [[38, 2, 33, 8, 17, 8, 39, 36, 34, 17, 26, 22, 10, 2, 37, 17, 33, 2, 23, 40, 38, 0, 40, 14, 3, 30],
[38, 20, 31, 33, 0, 30, 33, 2, 8, 34, 30, 36, 10, 2, 38, 35, 8, 40, 0, 25, 2, 30, 2, 25]]

This list has about 200 sub-lists (I've provided 2). 这个列表有大约200个子列表(我提供了2个)。 I am looking for a way to output an array containing a sliding window with an arbitrary before and after number of values. 我正在寻找一种方法来输出一个包含滑动窗口的数组,该窗口具有任意数值的前后值。 For example, with two preceding values and three following values, I am trying to get: 例如,有两个前面的值和三个后面的值,我试图得到:

[[_, _, 38, 2, 33, 8],
[_, 38, 2, 33, 8, 17],
[38, 2, 33, 8, 17, 9],
...,
[14, 3, 30, _, _, _,],
[_, _, 38, 20, 31, 33],
...]

I will need to repeat this operation many times (for different sub-lists), and thus speed is important. 我需要多次重复此操作(对于不同的子列表),因此速度很重要。 I am under the impression that converting the data to a Numpy array to use the answer here may be too slow (since each list is of a different length I am guessing I need to instantiate multiple np.arrays). 我的印象是将数据转换为Numpy数组以使用此处的答案可能太慢(因为每个列表的长度不同,我猜我需要实例化多个np.arrays)。 Is there a nice way to do this? 有一个很好的方法来做到这一点? Thank you! 谢谢!

I am fairly sure this is the fastest way of doing it. 我相信这是最快的方法。

def f(b):
    ret = []

    for sublist in b:
        ret.append(["_","_"] + sublist[0:4])
        ret.append(["_"] + sublist[0:5])
        for i in range(len(sublist)-4):
            ret.append(sublist[i:i+5])
        ret.append(sublist[-4:] + ["_"])
        ret.append(sublist[-3:] + ["_", "_"])
        ret.append(sublist[-2:] + ["_", "_", "_"])
    return ret

Obviously you have to iterate over every sublist in order to actually achieve the desired result, and for each sublist you have to iterate over it n times in order ot produce n lists, so I'm fairly sure this (o(n^2)) is the fastest you will get it. 显然你必须迭代每个子列表才能真正实现所需的结果,并且对于每个子列表,你必须迭代它n次以生成n个列表,所以我很确定这个(o(n ^ 2) )是你得到它最快的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建时间序列滑动 window tensorflow 数据集,其中某些特征的批量大小与其他特征不同? - How do I create a timeseries sliding window tensorflow dataset where some features have different batch sizes than others? Python-如何在文本文件中创建列的滑动窗口? - Python - How do I create a sliding window of columns in a text file? 如何创建一个滑动窗口来合并不同的条目? - How to create a sliding window for merging different entries? 如何创建滑动窗口生成器python 3.3 - How to create a sliding window generator python 3.3 如何为小部件创建不同的字体大小,但允许它们在PySide2中使用字体对话框缩放? - How do I create different font sizes for widgets but allow them to scale with font dialog in PySide2? 我怎样才能在 python 中制作这 9 个不同颜色和大小的不同球 - How can I make this 9 different balls with different colors and sizes in python 在 Python 中:如何重复创建和删除海龟 window? - In Python: how do I repeatedly Create and Delete a Turtle window? 如何在 python 中使用 tkinter 创建弹出窗口? - How do I create a pop up window using tkinter in python? 如何在 Python 的条件下应用滑动 window? - How to apply sliding window with conditions in Python? 如何遍历两个不同大小的数据帧? - How do I iterate through two dataframes of different sizes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM