B. Moore
B. Moore

Reputation: 109

Create List or Array of Specified Data Points

Simple question, may have already been answered. Looking to make a list or an array between two specified values. I want to also be able to control how many values are in the list.

For instance, lets say I want a list of values between 0 and pi, and I want the list to be 10 numbers long it would read something like

[0, 0.349, 0.698, 1.05, 1.40, 1.74, 2.09, 2.44, 2.79, 3.14]

Upvotes: 1

Views: 611

Answers (1)

jpp
jpp

Reputation: 164773

If you are happy to use a 3rd party library, numpy.linspace does the trick:

import numpy as np

res = np.linspace(0, np.pi, 10)

array([ 0.        ,  0.34906585,  0.6981317 ,  1.04719755,  1.3962634 ,
        1.74532925,  2.0943951 ,  2.44346095,  2.7925268 ,  3.14159265])

For a list, you can use res.tolist().

Upvotes: 2

Related Questions