展开列表中的数字 [英] Expand numbers in a list

查看:69
本文介绍了展开列表中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列表:

[10,20,30]

我需要的是根据预定义的增量来扩展它.因此,我们将x称为增量,将x=2称为我的结果:

What I need is to expand it according to a predefined increment. Thus, let's call x the increment and x=2, my result should be:

[10,12,14,16,18,20,22,24,.....,38]

现在我正在使用for循环,但是它非常慢,我想知道是否有更快的方法.

Right now I am using a for loop, but it is very slow and I am wondering if there is a faster way.

newA = []
for n in array:
    newA= newA+ generateNewNumbers(n, p, t) 

该函数生成新数字,只需生成新数字即可添加到列表中.

The function generates new number simply generate the new numbers to add to the list.

为了更好地定义问题,第一个数组包含一些时间戳:

To better define the problem the first array contains some timestamps:

[10,20,30]

我有两个参数,一个是采样率,一个是采样时间,我需要的是扩展数组,根据采样率在两个时间戳之间添加正确数量的时间戳. 例如,如果我的采样率为3,采样时间为3,则结果应为:

I have two parameters one is the sampling rate and one is the sampling time, what I need is to expand the array adding between two timestamps the correct number of timestamps, according to the sampling rate. For example, if I have a sampling rate 3 and a sampling time 3 the result should be:

[10,13,16,19,20,23,26,29,30,33,36,39]

推荐答案

您可以使用np.add.outer向每个时间戳添加相同的增量集,然后使用ravel展平结果.

You can add the same set of increments to each time stamp using np.add.outer and then flatten the result using ravel.

import numpy as np
a = [10,20,35]
inc = 3
ninc = 4
np.add.outer(a, inc * np.arange(ninc)).ravel()
# array([10, 13, 16, 19, 20, 23, 26, 29, 35, 38, 41, 44])

这篇关于展开列表中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆