简体   繁体   中英

Iterating over python lists: order of iteration

While iterating over lists, the order is from the zeroth element to the last. But for the return value of chebyt function of scipy, it is not clear to me how the iteration goes. Consider the following code:

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])

But iterating over index gives:

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

Whereas iterating over the list gives:

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

From printing of the Chebyshev polynomial or iterating over the list, the zeroth element seems to be 16 (coefficient of x^4), whereas by iterating over the coefficient index the zeroth element seems to be 0 (coefficient of x^0). Can someone explain this?

Coeffs[i] is the coefficient of the i -th power in the polynomial (see the documentation ).

If you'd like to iterate in the same order as displayed by repr() , iterate over Coeffs.c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM