2017-05-01 16 views
0

にループ:Pythonは、私は次のコードを持っている一行

from django.db.models import Sum, F, FloatField 
from data.models import Plant, Recording 
for plant in Plant.objects.all(): 
    total_ecof=0 
    total_ectr=0 
    for recording in Recording.objects.filter(plant__id=plant.id): 
     total_ecof = total_ecof + (recording.performance_loss*recording.multiplier*recording.occurrence*plant.no_modules)/(plant.nominal_power*recording.years_of_operation*plant.nominal_power) 
     total_ectr = total_ectr + ((10 + 15 + 200 + 50)*recording.occurrence+240*50*recording.occurrence*plant.no_modules)/(plant.nominal_power*3) 
    print(total_ecof) 
    print(total_ectr) 

結果は次のとおりです。

0.00018 
10800.049500000001 
0.0002666666666666667 
16000.073333333334 
6.666666666666667e-05 
4000.0183333333334 

質問は次のとおりです。 私は、単一の行のコードがあることをかなり確信しています同じことをして、私に知らせてください。

+1

確かに、問題は何ですか?コードが機能している場合、なぜ読みやすさを完全に破壊したいのですか? – Torxed

+1

あなたのコードが動作しているので、この質問はおそらくhttps://codereview.stackexchange.com/サイトに属していますか? –

答えて

0

あなたのlocal_ecof(またはlocal_ectr)を計算lambdaを定義する場合は、あなたがそれらすべてを合計するreduce(lambda x, y : x + y)を使用し、その後、リストのすべての要素に計算を適用するmap(lambda, list)機能を使用することができます。

関連する問題