迭代列表时,顺序是从第零个元素到最后一个元素。但是对于scipy的chebyt函数的返回值,我不清楚迭代是如何进行的。考虑以下代码:
from scipy.special import chebyt
import numpy as np
ChebyOrder = 5
Coeffs = chebyt(ChebyOrder)
print 'Chebyshev polynomial is: '+repr(Coeffs)
Chebyshev polynomial is: poly1d([ 1.60000000e+01, 5.32907052e-15, -2.00000000e+01,
-5.12827628e-15, 5.00000000e+00, 3.71174867e-16])
但是迭代索引给出:
L = len(Coeffs)
print '(1) Iterating over index: '
for i in range(L+1):
print Coeffs[i]
(1) Iterating over index:
3.71174867001e-16
5.0
-5.12827627586e-15
-20.0
5.3290705182e-15
16.0
而迭代列表给出:
print '(2) Iterating over list'
for c in Coeffs:
print c
(2) Iterating over list
16.0
5.3290705182e-15
-20.0
-5.12827627586e-15
5.0
3.71174867001e-16
从切比雪夫多项式的打印或迭代列表来看,第零个元素似乎是 16(x^4 的系数),而通过迭代系数索引,第零个元素似乎是 0(x^0 的系数) )。有人能解释一下吗?
最佳答案
Coeffs[i]
是多项式中的 i
次幂的系数(参见 documentation )。
如果您想按照 repr()
显示的顺序进行迭代,请迭代 Coeffs.c
。
关于python - 迭代Python列表: order of iteration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701111/