Ask Your Question

Revision history [back]

The indentation in your second code block is wrong, but if you fix that (don't indent while, indent all of the lines after it), it works, at least for me. The code can also be rewritten a few ways:

  • no need for the variable i:

    for a in List1: Result = List2[0].coefficient(a) List3.append(Result)

  • in fact, no need for an explicit loop at all, using list comprehension instead:

    List3 = [List2[0].coefficient(a) for a in List1]

By the way, there are other ways to get at the coefficients. For example:

a = x^2 + 2*y^2 + 3*z^2
for b in a:
    print(b)

will print

(1, x^2)
(2, y^2)
(3, z^2)

You can get the same information using list comprehension, [b for b in a] or in fact just list(a).

The indentation in your second code block is wrong, but if you fix that (don't indent while, indent all of the lines after it), it works, at least for me. The code can also be rewritten a few ways:

  • no

    No need for the variable i:

    for a in List1: 
        Result = List2[0].coefficient(a) 
        List3.append(Result) 

  • in List3.append(Result)

    In fact, no need for an explicit loop at all, using list comprehension instead:

    List3 = [List2[0].coefficient(a) for a in List1]

List1]

By the way, there are other ways to get at the coefficients. For example:

a = x^2 + 2*y^2 + 3*z^2
for b in a:
    print(b)

will print

(1, x^2)
(2, y^2)
(3, z^2)

You can get the same information using list comprehension, [b for b in a] or in fact just list(a).

The indentation in your second code block is wrong, but if you fix that (don't indent while, indent all of the lines after it), it works, at least for me. The code can also be rewritten a few ways:

No need for the variable i:

for a in List1: 
    Result = List2[0].coefficient(a) 
    List3.append(Result)

In fact, no need for an explicit loop at all, using list comprehension instead:

List3 = [List2[0].coefficient(a) for a in List1]

By the way, there are other ways to get at the coefficients. For example:

a = x^2 + 2*y^2 + 3*z^2
for b in a:
    print(b)

will print

(1, x^2)
(2, y^2)
(3, z^2)

You can get the same information using list comprehension, [b for b in a] , or in fact just list(a).