私のアプリケーションでは、基本的に "リスト内のすべての前の用語の和"を使用する必要がありますが、リストはリストではなくタプルのジェネレータです。インデックスは例としてジェネレータ式で "状態変数"を使用
「問題」1である(明らかに非動作するコードが、それはアイデアが表示されるはずです):
def calculate_minimal_charge_rate(self) -> float:
o = self.orbit
shadow_time = o.generate_shadow_light_time_list()
P = self.getIdlePowerConsumption()
a = 0
return max(a += ind[1]; P * ind[2].period/\
(ind[2].period - a) for ind in shadow_time)
または最小限の作業例(これはから別の機能として愚かであることに注意してくださいitertoolsは私のためにこれを解決することができた)
def calculate_minimal_charge_rate(self) -> float:
o = self.orbit
shadow_time = o.generate_shadow_light_time_list()
P = self.getIdlePowerConsumption()
a = 0
return max(a += ind[1]; a for ind in shadow_time)
今明白な「溶液」は、forループを使用することです:
def calculate_minimal_charge_rate(self) -> float:
o = self.orbit
shadow_time = o.generate_shadow_light_time_list()
P = self.getIdlePowerConsumption()
a = 0
max_power = 0
for ind in shadow_time:
a += ind[1]
preq = P * ind[2].period/\
(ind[2].period - a)
if preq > max_power:
max_power = preq
return max_power
しかし、このような単純な「もの」のためにあまりにも冗長その方法ではないでしょうか?ああ完全酒のために、ここにorbit.generate_shadow_light_time_list()
です:
def generate_shadow_light_time_list(self):
"""
Returns total max time in shadow
Iterativelly calls max_time_in_shadow for each orbit until star is found
"""
o = self
try:
while o.parent.brightness <= 0:
t = o.max_time_in_shadow()
yield (o.period - t, t, o)
o = o.parent.orbit
except AttributeError:
return
return
Pythonのタプルは '(値、値、値)'のように見えます –
ジェネレータの式はそれだけで、式でなければなりません。あなたはその中で代入をすることができません –
ここに蓄積の実装を見てください:https://docs.python.org/3/library/itertools.html#itertools.accumulate –