简体   繁体   English

如何将每个第 n 项保留在列表中并使其余项为零

[英]How to keep every nth item in a list and make the rest zeros

I am trying to model and fit to noisy data over a long time series and I want to see what happens to my fit if I remove a substantial amount of my data.我正在尝试对长时间序列的嘈杂数据进行建模和拟合,我想看看如果我删除了大量数据,我的拟合会发生什么。

I have a long time-series of data and I am only interested in every nth item.我有很长的时间序列数据,我只对第 n 个项目感兴趣。 However I still want to plot this list over time but with every other unwanted element removed.但是,我仍然想随着时间的推移绘制此列表,但删除所有其他不需要的元素。

For example, for n=4, the list例如,对于 n=4,列表

a = [1,2,3,4,5,6,7,8,9,10...]

Should become应该成为

a_new = [1,0,0,0,5,0,0,0,9,0...]

I don't mind if the position of the nth item is at the start or end of the sequence, my series is effectively arbitrary and so long that it won't matter what I delete.我不介意第 n 个项目的位置是在序列的开头还是结尾,我的系列实际上是任意的,而且时间长到我删除什么都无关紧要。 For example 'a_new' could also be:例如“a_new”也可以是:

a_new = [0,0,0,4,0,0,0,8,0,0...]

Ideally the solution wouldn't depend on the length of the list, but I can have that length as a variable.理想情况下,解决方案不依赖于列表的长度,但我可以将该长度作为变量。

Edit 1:编辑1:

I actually wanted empty elements, not zero's, (if that's possible?) so:我实际上想要空元素,而不是零,(如果可能的话?)所以:

a_new = [1,,,,5,,,,9...] 

Edit 2:编辑2:

I needed to remove the corresponding elements from my time series too so that when everything is plotted, each data element has the same index as the time series element.我还需要从我的时间序列中删除相应的元素,以便在绘制所有内容时,每个数据元素都具有与时间序列元素相同的索引。

Thanks!谢谢!

Use a list comprehension with a ternary conditional that takes the mod of each element on the number n : 使用具有三元条件列表理解 ,该条件采用数字n上每个元素的mod

>>> a = [1,2,3,4,5,6,7,8,9,10]
>>> n = 4
>>> [i if i % n == 0 else 0 for i in a]
[0, 0, 0, 4, 0, 0, 0, 8, 0, 0]

In case the data does not proceed incrementally, which is most likely, use enumerate so the mod is taken on the index and not on the element: 如果数据没有增量处理(很有可能),请使用enumerate以便将mod用作索引而不是元素:

>>> [v if i % n == 0 else 0 for i, v in enumerate(a)]
[1, 0, 0, 0, 5, 0, 0, 0, 9, 0]

The starting point can also be easily changed when using enumerate : 使用enumerate时,起点也可以轻松更改:

>>> [v if i % n == 0 else 0 for i, v in enumerate(a, 1)] # start indexing from 1
[0, 0, 0, 4, 0, 0, 0, 8, 0, 0]

If you intend to remove your unwanted data rather than replace them, then a filter using if (instead of the ternary operator) in the list comprehension can handle this: 如果您打算删除不需要的数据而不是替换它们,那么在列表推导中使用if (而不是三元运算符)的过滤器可以处理以下问题:

>>> [v for i, v in enumerate(a, 1) if i % n == 0]
[4, 8]
[0 if i%4 else num for i, num in enumerate(a)]

Here's a working example to filter functions given a certain step K: 这是一个给定步骤K过滤功能的工作示例:

def filter_f(data, K=4):
    if K <= 0:
        return data

    N = len(data)
    f_filter = [0 if i % K else 1 for i in range(N)]
    return [a * b for a, b in zip(data, f_filter)]

f_input = range(10)

for K in range(10):
    print("Original function: {0}".format(f_input))
    print("Filtered function (step={0}): {1}".format(
        K, filter_f(f_input, K)))
    print("-" * 80)

Output: 输出:

Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=2): [0, 0, 2, 0, 4, 0, 6, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=3): [0, 0, 0, 3, 0, 0, 6, 0, 0, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=4): [0, 0, 0, 0, 4, 0, 0, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=5): [0, 0, 0, 0, 0, 5, 0, 0, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=6): [0, 0, 0, 0, 0, 0, 6, 0, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=7): [0, 0, 0, 0, 0, 0, 0, 7, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=8): [0, 0, 0, 0, 0, 0, 0, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=9): [0, 0, 0, 0, 0, 0, 0, 0, 0, 9]
--------------------------------------------------------------------------------

Alternatively, irrespective of the programming language, you can use function:或者,无论编程语言如何,您都可以使用函数:

$f(i)=(1-(-1)^{\floor((i-1)/k)+\floor(i/k)})/2$

This function produces 1 every k-th element.此函数每第 k 个元素产生 1 个。 For k=4, this generates对于 k=4,这会生成

f(i)=[0,0,0,1,0,0,0,1,0,0,0,1] for i=[1,2,3,4,5,6,7,8,9,10,11,12]

The function that you want would be then i*f(i) .你想要的函数就是i*f(i)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM