2017-05-29 8 views
0

私はPythonで奇妙な問題を抱えています。 (値が印刷で表示されない

EV1, Current SOC: 0.44, required power: 15.1, allocated power: 0.15638636639, max power: 3.3 

EV2, Current SOC: 0.9, required power: 1.0, allocated power: 1.0, max power: 1.0 

EV4, Current SOC: 0.92, required power: 6.5, allocated power: 3.3, max power: 3.3 

あなたが見ることができるように、一つの値:

for cd in self.current_charging_demands: 
     print"EV{}".format(cd.id) 

    # Check the value of SOC. 
    for cd in self.current_charging_demands: 
     print"EV{}, Current SOC: {}, required power: {}, allocated power: {}, max power: {}\n".format(cd.id, round(cd.battery.current_soc, 2), cd.battery.power_to_charge, cd.battery.allocated_powers, cd.battery.max_power) 
     if round(cd.battery.current_soc, 2) >= cd.battery.desired_soc: 
      #print"EV{} - current SOC: {}".format(cd.id, cd.battery.current_soc) 
      result.append(cd) 
      db.delete_charging_demand(self.current_charging_demands, cd.id) 

最初に印刷しているため、これらの値:

EV1 
EV2 
EV5 
EV4 

は二番目はこれらを印刷している私は、次のコードを持っていますEV5)が2番目に欠けているのはなぜなのでしょうか。 forは両方のループの間で変更されていない同じオブジェクトに対して行われます。

EV1 
EV5 
EV4 
最初のforループ

と:これらの関数の次の呼び出しで、私は次の値を取得しています

EV1, Current SOC: 0.44, required power: 15.0, allocated power: 0.15638636639, max power: 3.3 

EV5, Current SOC: 0.35, required power: 23.7, allocated power: 0.0, max power: 3.3 

EV4, Current SOC: 0.92, required power: 3.2, allocated power: 0.0, max power: 3.2 

何が起こっている上の任意のアイデア?

ありがとうございました。あなたのコードで

答えて

3

反復処理中に要素を削除するときには非常に注意しなければならないライン

db.delete_charging_demand(self.current_charging_demands, cd.id)

があります。いくつかの要素はスキップすることができます。

これを確認するには、次のコードを実行してみてください。

a = [1, 2, 3, 4] 
for x in a: 
    print(x) 
    if x == 2: 
     a.remove(x) 

これは1 2 4を出力し、3が欠落しています。

これを解決するには、this postがあります。

+0

ありがとう、それは今動作します! – Ecterion

関連する問題