2

Python 3.2.3 IDLE を使用しています。reduce コマンドを使用している人がいますが、何らかの理由でそれを持っていません。コードが紫色で表示されず、reduce が変数として認識されるように。

ここに私のコードの一部があります:

numbers = [10, 11, 11]
numbertotal = (set(numbers))
#removes duplicates in my list, therefore, the list only contains [10, 11]
print ("The sum of the list is", (sum(numbertotal))) #sum is 21
print ("The product of the list is" #need help here, basically it should be 10 * 11 = 110

の重複を削除した後、基本的にリストを増やしたいと思いnumbertotalます。

4

2 に答える 2

3

あなたのreduce隠れ場所:

from functools import reduce

print("The product of the list is", reduce(lambda x,y:x*y, numbertotal))

また

from functools import reduce
import operator as op

print("The product of the list is", reduce(op.mul, numbertotal))

python3 では に移動されましたfunctoolsがこの2to3ケースを処理します。

于 2012-12-05T13:31:25.793 に答える
0

これはうまくいきますか?

product = 1
for j in numbertotal:
    product = product * j
print 'The product of the list is', product
于 2012-12-05T13:33:02.253 に答える