Skip to content Skip to sidebar Skip to footer

Expand Numbers In A List

I have a list of numbers: [10,20,30] 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,1

Solution 1:

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])

Solution 2:

You can use list comprhensions but I'm not sure if I understand the stopping condition for the last point inclusion

a = [10, 20, 30, 40]
t = 3
sum([[x for x in range(y, z, t)] for y, z in zip(a[:-1], a[1:])], []) + [a[-1]]

will give

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

Solution 3:

Using range and itertools.chain

l = [10,20,30]
x = 3from itertools import chain
list(chain(*[range(i,i+10,x) for i in l]))
#Output:#[10, 13, 16, 19, 20, 23, 26, 29, 30, 33, 36, 39]

Solution 4:

Here is a bunch of good answers already. But I would advise numpy and linear interpolation.

# Now, this will give you the desired result with your first specifications# And in pure Python too
t = [10, 20, 30]
increment = 2
last = int(round(t[-1]+((t[-1]-t[-2])/float(increment))-1)) # Value of last number in array# Note if you insist on mathematically "incorrect" endpoint, do:#last = ((t[-1]+(t[-1]-t[-2])) -((t[-1]-t[-2])/float(increment)))+1
newt = range(t[0], last+1, increment)
# And, of course, this may skip entered values (increment = 3# But what you should do instead, when you use the samplerate is# to use linear interpolation# If you resample the original signal,# Then you resample the time too# And don't expand over the existing time# Because the time doesn't change if you resampled the original properly# You only get more or less samples at different time points# But it lasts the same length of time.# If you do what you originally meant, you actually shift your datapoints in time# Which is wrong.import numpy

t = [10, 20, 30, 40, 50, 60]
oldfs = 4000# 4 KHz samplerate
newfs = 8000# 8 KHz sample rate (2 times bigger signal and its time axis)
ratio = max(oldfs*1.0, newfs*1.0)/min(newfs, oldfs)
newlen = round(len(t)*ratio)
numpy.interp(
    numpy.linspace(0.0, 1.0, newlen),
    numpy.linspace(0.0, 1.0, len(t)),
    t)

This code can resample your original signal too (if you have one). If you just want to cram in some more timepoints in between, you can also use interpolation. Again, don't go over the existing time. Although this code does it, to be compatible with the first one. And so that you can get ideas on what you can do.

t = [10, 20, 30]
increment = 2
last = t[-1]+((t[-1]-t[-2])/float(increment))-1 # Value of last number in array
t.append(last)
newlen = (t[-1]-t[0])/float(increment)+1 # How many samples we will get in the end
ratio = newlen / len(t)
numpy.interp(
    numpy.linspace(0.0, 1.0, newlen),
    numpy.linspace(0.0, 1.0, len(t)),
    t)

This though results in an increment of 2.5 instead of 2. But it can be corrected. The thing is that this approach would work on floating time points as well as on integers. And fast. It will slow down if there is a lot of them, but until you reach some great number of them it will work pretty fast.

Post a Comment for "Expand Numbers In A List"