PdF
PdF

Reputation: 77

Creating list of five elements from a List of Number in Python

i'm using Python 3.7.

I have a tuple of Numbers like this:

 x = ((1,2,3,4,5,6,7,8,9....etc))

I would Like to obtain a list of list divided by 100 and with Five numbers from the list in an iterative way... something like this:

[[[0.0], [0.01], [0.02], [0.03], [0.04]],
[[0.01], [0.02], [0.03], [0.04], [0.05]],
[[0.02], [0.03], [0.04], [0.05], [0.06]],
[[0.03], [0.04], [0.05], [0.06], [0.07]],
[[0.04], [0.05], [0.06], [0.07], [0.08]],
[[0.05], [0.06], [0.07], [0.08], [0.09]],... etc

I Tried this but it doesn't work properly:

Data = [[[(interest_over_time_data+j)/100] for 
interest_over_time_data in range(5)]for j in 
interest_over_time_data]

The real numbers are not a list of consecutive number so I cannot add +1 to each element...

Thank you in advance!

Upvotes: 2

Views: 3862

Answers (4)

jpp
jpp

Reputation: 164653

You can use 3rd party NumPy for an array-based solution:

import numpy as np

first_row = np.arange(5) / 100
first_col = np.arange(10) / 100

res = first_row + first_col[:, None]

array([[ 0.  ,  0.01,  0.02,  0.03,  0.04],
       [ 0.01,  0.02,  0.03,  0.04,  0.05],
       [ 0.02,  0.03,  0.04,  0.05,  0.06],
       [ 0.03,  0.04,  0.05,  0.06,  0.07],
       [ 0.04,  0.05,  0.06,  0.07,  0.08],
       [ 0.05,  0.06,  0.07,  0.08,  0.09],
       [ 0.06,  0.07,  0.08,  0.09,  0.1 ],
       [ 0.07,  0.08,  0.09,  0.1 ,  0.11],
       [ 0.08,  0.09,  0.1 ,  0.11,  0.12],
       [ 0.09,  0.1 ,  0.11,  0.12,  0.13]])

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

  • You want a list of lists, that calls for a double list comprehension.
  • You want sliding windows, that calls for slicing, better done with itertools.islice

this code below creates 5 sliding sublists with 100 division.

import itertools

x = (1,2,3,4,5,6,7,8,9)
result = [[v/100.0 for v in itertools.islice(x,start,start+5)] for start in range(6)]

result:

[[0.01, 0.02, 0.03, 0.04, 0.05], 
 [0.02, 0.03, 0.04, 0.05, 0.06], 
 [0.03, 0.04, 0.05, 0.06, 0.07], 
 [0.04, 0.05, 0.06, 0.07, 0.08], 
 [0.05, 0.06, 0.07, 0.08, 0.09],
 [0.06, 0.07, 0.08, 0.09]]

Upvotes: 2

JakobHeltzig
JakobHeltzig

Reputation: 335

Okay, you want your output containing lists with a length of 5 shifting from the first element of x to the last. Therefore your output will contain n-4 lists, where n is len(x).

So first we need to iterate over range(len(x)-4)

Then we want five elements from x starting at a given offset i. We can use slicing for this, e.g. x[i:i+5].

And we want all elements of this sublist divided by 100.

All together packed in list comprehension it looks like this:

x = (1,2,3,4,5,6,7,8,9)
res = [
    [j/100.0 for j in x[i:i+5]]
        for i in range(len(x)-4)
]
print(res)

Which results in

[[0.01, 0.02, 0.03, 0.04, 0.05],
 [0.02, 0.03, 0.04, 0.05, 0.06],
 [0.03, 0.04, 0.05, 0.06, 0.07],
 [0.04, 0.05, 0.06, 0.07, 0.08],
 [0.05, 0.06, 0.07, 0.08, 0.09]]

Or if you want to have 0.0 as in your example output:

x = (1,2,3,4,5,6,7,8,9)
x = [0] + list(x)
res = [
    [j/100.0 for j in x[i:i+5]]
        for i in range(len(x)-4)
]
print(res)

Upvotes: 1

ibarrond
ibarrond

Reputation: 7591

Love one line solutions:

[[[x[p]/100] for p in range(k,k+5)] for k in range(len(x)-4)]

#>[[[0.01], [0.02], [0.03], [0.04], [0.05]],
#> [[0.02], [0.03], [0.04], [0.05], [0.06]],
#> [[0.03], [0.04], [0.05], [0.06], [0.07]],
#> [[0.04], [0.05], [0.06], [0.07], [0.08]],
#> [[0.05], [0.06], [0.07], [0.08], [0.09]]]

Upvotes: 1

Related Questions