Basically I want to go from -1 to 1 in n
steps, including -1 and 1:
基本上我想在-1步骤中从-1到1,包括-1和1:
x = -1.0
n = 21
for i in range(n):
print x
x += 0.01
-1.0 -0.9 -0.8 ... 0.8 0.9 1.0
How can I write this in the most elegant, simplest way for any n
value?
我怎样才能以最优雅,最简单的方式为任何n值写这个?
5 个解决方案
#1
5
startValue = -1.0
intervalLength = 2.0
numberOfSteps = 14
increasePerStep = intervalLength / numberOfSteps
print startValue
x = startValue
for i in range (numberOfSteps):
x += increasePerStep
print x
-1.0
-0.857142857143
-0.714285714286
-0.571428571429
-0.428571428571
-0.285714285714
-0.142857142857
-2.22044604925e-16
0.142857142857
0.285714285714
0.428571428571
0.571428571429
0.714285714286
0.857142857143
1.0
#2
11
There is no built-in solution, but probably a good way to solve it is to define your own range
function:
没有内置的解决方案,但解决它的一个好方法是定义自己的范围函数:
def my_range(start, end, how_many):
incr = float(end - start)/(how_many - 1)
return [start + i*incr for i in range(how_many)]
And you can use it in a for-loop:
你可以在for循环中使用它:
>>> for i in my_range(-1, 1, 10):
... print i
...
-1.0
-0.777777777778
-0.555555555556
-0.333333333333
-0.111111111111
0.111111111111
0.333333333333
0.555555555556
0.777777777778
1
EDIT: As @NiklasBaumstark suggested, if your brand new my_range
function is going to handle a big quantity of numbers it is probably a good idea to use generators. For that purpose, we'll do just a little modification:
编辑:正如@NiklasBaumstark建议的那样,如果你的全新my_range函数将处理大量数字,那么使用生成器可能是个好主意。为此,我们只做一点修改:
def my_xrange(start, end, how_many):
incr = float(end - start)/(how_many - 1)
return (start + i*incr for i in xrange(how_many))
#3
11
If it's OK to use numpy
, this works fine:
如果可以使用numpy,这可以正常工作:
import numpy as np
n = 21
for i in np.linspace(-1, 1, n):
print i
#4
3
I would go with
我会去
for x in xrange(n):
print float(2*x)/(n-1) - 1
Two things of interest: xrange
is more efficient than range
, and there's no need to have two separate iterator variables.
有两件事:xrange比range更有效,并且不需要有两个独立的迭代器变量。
You could wrap this in a function if you find it frequently useful:
如果您发现它经常有用,您可以将其包装在一个函数中:
def linspace1(n):
for x in xrange(n):
yield float(2*x)/(n-1) - 1
although you'd probably want to make the lower and upper limits parameters as well, as in julio.alegria's answer.
虽然您可能也想制作下限和上限参数,如julio.alegria的答案。
#5
2
You said you had your answer, but I feel that this is an elegant solution. It also eliminates the possibility of weird floating-point issues (at least, from the defined range). It combines the power and flexibility of generators along with string formatting and floating-point values. Alternatively, you could avoid much of this if you elected to go with the Decimal
module - but that would require a bit more tweaking.
你说你有答案,但我觉得这是一个优雅的解决方案。它还消除了出现奇怪浮点问题的可能性(至少从定义的范围)。它结合了生成器的功能和灵活性以及字符串格式和浮点值。或者,如果你选择使用Decimal模块,你可以避免这么做 - 但这需要更多的调整。
def decimal_stepper(start, end, step=0.1):
while start <= end:
yield float(start)
start = float(('%' + str(step) + 'f') % (start+step))
An example run:
一个示例运行:
myVals = decimal_stepper(-1, 1)
for x in myVals:
print x
Which outputs:
哪个输出:
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
#1
5
startValue = -1.0
intervalLength = 2.0
numberOfSteps = 14
increasePerStep = intervalLength / numberOfSteps
print startValue
x = startValue
for i in range (numberOfSteps):
x += increasePerStep
print x
-1.0
-0.857142857143
-0.714285714286
-0.571428571429
-0.428571428571
-0.285714285714
-0.142857142857
-2.22044604925e-16
0.142857142857
0.285714285714
0.428571428571
0.571428571429
0.714285714286
0.857142857143
1.0
#2
11
There is no built-in solution, but probably a good way to solve it is to define your own range
function:
没有内置的解决方案,但解决它的一个好方法是定义自己的范围函数:
def my_range(start, end, how_many):
incr = float(end - start)/(how_many - 1)
return [start + i*incr for i in range(how_many)]
And you can use it in a for-loop:
你可以在for循环中使用它:
>>> for i in my_range(-1, 1, 10):
... print i
...
-1.0
-0.777777777778
-0.555555555556
-0.333333333333
-0.111111111111
0.111111111111
0.333333333333
0.555555555556
0.777777777778
1
EDIT: As @NiklasBaumstark suggested, if your brand new my_range
function is going to handle a big quantity of numbers it is probably a good idea to use generators. For that purpose, we'll do just a little modification:
编辑:正如@NiklasBaumstark建议的那样,如果你的全新my_range函数将处理大量数字,那么使用生成器可能是个好主意。为此,我们只做一点修改:
def my_xrange(start, end, how_many):
incr = float(end - start)/(how_many - 1)
return (start + i*incr for i in xrange(how_many))
#3
11
If it's OK to use numpy
, this works fine:
如果可以使用numpy,这可以正常工作:
import numpy as np
n = 21
for i in np.linspace(-1, 1, n):
print i
#4
3
I would go with
我会去
for x in xrange(n):
print float(2*x)/(n-1) - 1
Two things of interest: xrange
is more efficient than range
, and there's no need to have two separate iterator variables.
有两件事:xrange比range更有效,并且不需要有两个独立的迭代器变量。
You could wrap this in a function if you find it frequently useful:
如果您发现它经常有用,您可以将其包装在一个函数中:
def linspace1(n):
for x in xrange(n):
yield float(2*x)/(n-1) - 1
although you'd probably want to make the lower and upper limits parameters as well, as in julio.alegria's answer.
虽然您可能也想制作下限和上限参数,如julio.alegria的答案。
#5
2
You said you had your answer, but I feel that this is an elegant solution. It also eliminates the possibility of weird floating-point issues (at least, from the defined range). It combines the power and flexibility of generators along with string formatting and floating-point values. Alternatively, you could avoid much of this if you elected to go with the Decimal
module - but that would require a bit more tweaking.
你说你有答案,但我觉得这是一个优雅的解决方案。它还消除了出现奇怪浮点问题的可能性(至少从定义的范围)。它结合了生成器的功能和灵活性以及字符串格式和浮点值。或者,如果你选择使用Decimal模块,你可以避免这么做 - 但这需要更多的调整。
def decimal_stepper(start, end, step=0.1):
while start <= end:
yield float(start)
start = float(('%' + str(step) + 'f') % (start+step))
An example run:
一个示例运行:
myVals = decimal_stepper(-1, 1)
for x in myVals:
print x
Which outputs:
哪个输出:
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0