了解 Python 元组和重新分配
Understanding Python Tuples and Reassignments
好的,我认为这个问题以前没有在这里回答过。
我想知道 Python 是如何执行这个 for 循环的。仅供参考,这是 6.00SC MIT OCW 课程 2 的一部分:
def evaluate_poly(poly, x):
""" Computes the polynomial function for a given value x. Returns that value.
Example:
>>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2
>>> x = -13
>>> print evaluate_poly(poly, x) # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2
180339.9
poly: tuple of numbers, length > 0
x: number
returns: float """
ans = 0.0
for i in xrange(len(poly)):
ans += poly[i] * (x ** i)
return ans
任何人都可以向我解释一下这个 for 循环是如何逐行执行的吗?我知道 i 变量被创建为 运行 5 次(poly 元组的长度),其中每次迭代都会更新 ans。我感到困惑的是每次重新分配 i 。
第三次通过ans = 0.0 + (5) * x**(2)
在我看来,poly[i] 正在获取索引数 (5),但随后将 x 乘以 i 的幂,即现在的索引位置本身 (2)。这正是它应该做的,但是我不明白我怎么可能看起来既是索引数字又是索引位置。
我是编程新手,所以任何信息都会有很大的帮助。
非常感谢!
i 被分配给循环中的那些数字:0,1,2,3,4 因为 xrange 创建一个从 0 到参数负 1 的范围。参数是 len(poly) that returns 5 (数组的大小。因此 i 从 0 到 4(=5-1)
第一次迭代 i 等于 0:
poly[0]其实等于poly(0.0)的第一个元素
公式则变为:
ans += poly[i] * (x ** i)
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[0] * (-13 in the power of 0)
ans = 0.0 + 0.0 * (-13 in the power of 0)
ans = 0.0
下一次迭代 i 等于 1:
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[1] * (-13 in the power of 1)
ans = 0.0 + 0.0 * (-13 in the power of 1)
ans = 0.0
下一次迭代 i 等于 2:
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[2] * (-13 in the power of 2)
ans = 0.0 + 5.0 * (-13 in the power of 2)
下一次迭代 i 等于 3:
ans = ans + poly[i] * (x ** i)
ans = 5.0 * (-13 in the power of 2) + poly[3] * (-13 in the power of 3)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3)
最后一次迭代 i 等于 4:
ans = ans + poly[i] * (x ** i)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3) + poly[4] * (-13 in the power of 4)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3) + 7.0 * (-13 in the power of 4)
好的,我认为这个问题以前没有在这里回答过。
我想知道 Python 是如何执行这个 for 循环的。仅供参考,这是 6.00SC MIT OCW 课程 2 的一部分:
def evaluate_poly(poly, x):
""" Computes the polynomial function for a given value x. Returns that value.
Example:
>>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2
>>> x = -13
>>> print evaluate_poly(poly, x) # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2
180339.9
poly: tuple of numbers, length > 0
x: number
returns: float """
ans = 0.0
for i in xrange(len(poly)):
ans += poly[i] * (x ** i)
return ans
任何人都可以向我解释一下这个 for 循环是如何逐行执行的吗?我知道 i 变量被创建为 运行 5 次(poly 元组的长度),其中每次迭代都会更新 ans。我感到困惑的是每次重新分配 i 。
第三次通过ans = 0.0 + (5) * x**(2)
在我看来,poly[i] 正在获取索引数 (5),但随后将 x 乘以 i 的幂,即现在的索引位置本身 (2)。这正是它应该做的,但是我不明白我怎么可能看起来既是索引数字又是索引位置。
我是编程新手,所以任何信息都会有很大的帮助。
非常感谢!
i 被分配给循环中的那些数字:0,1,2,3,4 因为 xrange 创建一个从 0 到参数负 1 的范围。参数是 len(poly) that returns 5 (数组的大小。因此 i 从 0 到 4(=5-1)
第一次迭代 i 等于 0:
poly[0]其实等于poly(0.0)的第一个元素
公式则变为:
ans += poly[i] * (x ** i)
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[0] * (-13 in the power of 0)
ans = 0.0 + 0.0 * (-13 in the power of 0)
ans = 0.0
下一次迭代 i 等于 1:
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[1] * (-13 in the power of 1)
ans = 0.0 + 0.0 * (-13 in the power of 1)
ans = 0.0
下一次迭代 i 等于 2:
ans = ans + poly[i] * (x ** i)
ans = 0.0 + poly[2] * (-13 in the power of 2)
ans = 0.0 + 5.0 * (-13 in the power of 2)
下一次迭代 i 等于 3:
ans = ans + poly[i] * (x ** i)
ans = 5.0 * (-13 in the power of 2) + poly[3] * (-13 in the power of 3)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3)
最后一次迭代 i 等于 4:
ans = ans + poly[i] * (x ** i)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3) + poly[4] * (-13 in the power of 4)
ans = 5.0 * (-13 in the power of 2) + 9.3 * (-13 in the power of 3) + 7.0 * (-13 in the power of 4)