2014-09-06 149 views
1

在遍历列表时,顺序是从第零个元素到最后一个。但是对于scipy的chebyt函数的返回值,我不清楚迭代是如何进行的。请看下面的代码:迭代python列表:迭代顺序

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)。有人可以解释这个吗?

回答

1

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

如果您想按与repr()显示的顺序进行迭代,请重复执行Coeffs.c

+0

谢谢,这是有道理的。 – MaviPranav 2014-09-06 14:26:34