2

I want to do simple lists multiplication. It works good for c but fails for d with following error: TypeError: zip argument #1 must support iteration

Any suggestion to correct it would be appreciative.

    x=[]
    area1 = (area1)/100
    area2 = (area2)/100
    area3 = (area3)/100
    x.append(area1)
    x.append(area2)
    x.append(area3)
    # resultant x is [0.96, 0.03, 0.0]

    a = [13.87, 14.78, 10.3]
    b = [0.44, 0.39, 0.38]

    c = sum([x* a for x, a in zip(x, a)])
    d = sum([x* b for x, b in zip(x, b)])
4

1 回答 1

1

You must use names other than x, a and b in your list comprehensions:

c = sum([_x * _a for _x, _a in zip(x, a)])
d = sum([_x * _b for _x, _b in zip(x, b)])

Yours is re-assigning x to be the first element of x and failing on the second usage of zip with x.

You can instead use map and operator.mul:

from operator import mul
c = sum(map(mul, x, a))
d = sum(map(mul, x, b))
于 2013-08-21T01:57:14.283 回答