0

I have a problem discretizing in python.

I have a list st:

st=[(-0.8,0.8),(-0.5,0.5),(-0.104,0.104),(-0.872,0.872)]

I would like to discretize 'st' into a number of parts determined by a different list. In other words, I would like to divide each element of the list into n parts.

For example, I would like to divide by the elements of the list b=(3,3,6,2). Therefore (-0.8,0.8) will be divided into 3 parts. (-0.5,0.5) would be split into 3 parts and so on.

The output should look like st=[[(-0.8,-0.2),(-0.2,0.2),(0.2,0.8)),....]] thank you.

Zev
  • 3,423
  • 1
  • 20
  • 41
Stevy KUIMI
  • 47
  • 2
  • 6
  • 2
    can you please update your question with expected output? – Murali Jun 07 '18 at 02:33
  • 1
    Sounds like you need [np.linspace](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html). – YSelf Jun 08 '18 at 14:38
  • Why `(-0.8,-0.2),(-0.2,0.2),(0.2,0.8)`? Shouldn't it be `(-0.8,-0.2666666666666666),(-0.2666666666666666666,0.26666666666666666),(0.2666666666666666,0.8)` so all the partitions are the same size? And if so, how will you handle rounding errors? – Tim Pietzcker Jun 08 '18 at 14:48
  • Hi, I only took the first decimal digit. but yeah you are right, to be precise it should like you have written it, same size, not necessarily. the number of partition is what I am trying to achieve. – Stevy KUIMI Jun 08 '18 at 18:02

1 Answers1

0

I think there are two issues that can be dealt with separately:

How do you get evenly spaced intervals from Python

For that, let us look at a simpler example:

import numpy as np
a = (-0.8,0.8)

b = 3
c = np.linspace(a[0], a[1], b + 1)
d = list(zip(c, c[1:]))
print(d)

Which outputs:

[
    (-0.80000000000000004, -0.26666666666666672),
    (-0.26666666666666672, 0.26666666666666661),
    (0.26666666666666661, 0.80000000000000004)
]

How do you repeat the above procedure with the given data structures

st=[(-0.8,0.8),(-0.5,0.5),(-0.104,0.104),(-0.872,0.872)]

b=(3,3,6,2)

result = []
for start_stop, parts in  zip(st, b):
    start, stop = start_stop
    c = np.linspace(start, stop, parts + 1)
    d = list(zip(c, c[1:]))
    result.append(d)

print(result)

Which results in:

[
    [
        (-0.80000000000000004, -0.26666666666666672), 
        (-0.26666666666666672, 0.26666666666666661), 
        (0.26666666666666661, 0.80000000000000004)
    ], 
    [
        (-0.5, -0.16666666666666669), 
        (-0.16666666666666669, 0.16666666666666663),
        (0.16666666666666663, 0.5)
    ], 

and so on...

Zip matches elements from one list to another and lets you loop over them together so that's very useful here.

NumPy's Linear Spacing function (np.linspace) is the operation you want to perform. See details here

Zev
  • 3,423
  • 1
  • 20
  • 41
  • Thank you very much, exactly what I was looking for and it works for different value of b. God bless you – Stevy KUIMI Jun 12 '18 at 14:58
  • if i have 4 variables, x,y,v,w, knowing that st=[(-0.8,0.8),(-0.5,0.5),(-0.104,0.104),(-0.872,0.872)], x is in the first range, y in the second, and so on, – Stevy KUIMI Jun 12 '18 at 15:11