迭代python列表:迭代的顺序

时间:2014-09-06 14:08:53

标签: python list loops scipy

在迭代列表时,顺序是从第0个元素到最后一个元素。但是对于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

从Chebyshev多项式的打印或迭代列表,第0个元素似乎是16(x ^ 4的系数),而通过迭代系数索引,第0个元素似乎是0(x ^ 0的系数) )。有人可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

Coeffs[i]是多项式中i次幂的系数(参见documentation)。

如果您希望以repr()显示的顺序进行迭代,请迭代Coeffs.c

相关问题