簡體   English   中英

蟒蛇循環到一行

[英]python a loop to a single line

我有以下代碼:

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

問題是:我很確定只有一行代碼可以做到這一點,請告訴我。

如果定義用於計算local_ecof(或local_ectr)的lambda ,則可以使用map(lambda, list)函數將計算應用於列表的所有元素,然后使用reduce(lambda x, y : x + y)總結所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM