2016-07-29 12 views
0

Ok, ich glaube nicht, dass diese Frage hier schon einmal beantwortet wurde.Python-Tupel und Neuzuordnungen verstehen

Ich frage mich genau, wie Python diese for-Schleife ausführt. Zu Ihrer Information ist dies ein Teil der Lektion 2 von 6.00SC MIT OCW:

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 

Kann jemand mir erklären, wie dies für die Schleife zeilen ausgeführt wird? Ich verstehe, dass die i-Variable erstellt wird, um 5 Mal (die Länge des Poly-Tupels) zu laufen, in dem AN jede Iteration aktualisiert wird. Wo ich verwirrt bin, ist die Neuzuordnung von mir jedes Mal durch.

Das dritte Mal durch am = 0,0 + (5) * x ** (2)

Es scheint mir, dass Poly [i] wird die indizierte Zahl Grabbing (5), aber dann ist x multipliziert die Macht von i, die jetzt die Indexposition selbst ist (2). Das ist genau das, was es tun soll, aber ich kann nicht verstehen, wie ich sowohl die indexierte Nummer als auch die indizierte Position sein kann.

Ich bin neu in der Programmierung, so dass jede Info überhaupt eine enorme Hilfe sein wird.

Vielen Dank!

+0

in der Zeile vor 'a ns = 0.0 'put' importieren pdb; pdb.set_trace() '. [Hier ist ein gutes Tutorial zum Thema] (https://pythonconquersstheuniverse.wordpress.com/2009/09/10/debugging-in-python/) –

+0

'x ** i' bedeutet" x an die Macht von i ", Das ist ziemlich genau das, was Sie in einem Polynom erwarten würden. – khelwood

+0

'x ** i' ist nicht' x' multipliziert mit 'i'. Es ist 'x' zur Kraft von' i'. –

Antwort

1

i wird diesen Zahlen in der Schleife zugewiesen: 0,1,2,3,4 weil xrange einen Bereich von 0 bis zum Parameter minus 1 erzeugt. Parameter ist len ​​(poly), der 5 zurückgibt (die Größe der .-Array wird daher i zugeordnet, von 0 bis 4 (= 5-1)

erster Iteration i gleich 0:

Poly [0] gleich der tatsächlich auf das erste Element von Poly (0,0)

The Formel wird dann:

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

nächste Iteration gleich 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 

nächste Iteration i gleich 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) 

nächste Iteration i gleich 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) 

Letzte Iteration i gleich 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) 
+0

Genau das habe ich gesucht. Danke für die detaillierte Klärung. – Chris

Verwandte Themen