python - 如何关联两个列表?

标签 python python-3.x list python-2.7

我有两个列表和一些参数。在这些输入的帮助下,我可以对电池进行充电和放电。第一个列表中有 0 和 1。 0 表示对电池放电,1 表示对电池充电。在第二个列表中,有一些值,在它们的帮助下我们可以为电池充电。 电池参数为:

Max_capacity = 600MW
initial_capacity = 0MW
Duration = 4hours
battery size = 150MW

两个列表中实际上都有 8760 个值,但我只显示了几个

signal = [1,1,1,1,0,0,0,0,0,1,1,1,0,0]
excess_energy = [0,100,90,160,20,0,0,0,0,0,50,60,70,0]

所以这里有几个条件:

  1. 只有当信号为1并且excess_energy大于0时电池才能充电。
  2. 如果 extra_energy 大于 150MW,则电池将仅以 150MW 充电,如果小于,则可以以任何多余能量值充电。
  3. 电池只有在信号为0时才能放电,固定时间150MW。
  4. 电池不能放电到0以下,不能充电超过600MW 预期输出:

    enter image description here

我的方法:

battery_size = 150
duration = 4
initial_capacity = 0
max_capacity = 600
ans_charged = []
ans_discharged = []
discharge = 0
days_counter = 0
signal = [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]  # Reversed the polarity
excess_energy = [0, 100, 90, 160, 20, 0, 0, 0, 0, 0, 50, 60, 70, 0]
for decider, ex_energy in zip(signal, excess_energy):
    days_counter += 1  # days counter because every new day all the counters are reset
    if decider == 1 and discharge < duration and max_capacity >= initial_capacity >=0:  # at 0 we charging and at 1 we are discharging
        initial_capacity -= battery_size
        if initial_capacity >= 0:
            ans_charged.append(initial_capacity)
            ans_discharged.append(0)
            discharge += 1
        else:
            ans_charged.append(0)
            ans_discharged.append(0)
            discharge += 1
    elif decider == 1:
        ans_charged.append(0)
        ans_discharged.append(0)
    elif decider == 0 and ex_energy > 0:
        if ex_energy < 150 and max_capacity >= initial_capacity >= 0:
            initial_capacity += ex_energy
            if initial_capacity <= max_capacity:
                ans_discharged.append(initial_capacity)
                ans_charged.append(0)
            else:
                ans_discharged.append(max_capacity)
                ans_charged.append(0)
        else:
            initial_capacity += 150
        if initial_capacity <= max_capacity:
            ans_discharged.append(initial_capacity)
            ans_charged.append(0)
        else:
            ans_discharged.append(max_capacity)
            ans_charged.append(0)
    elif decider == 0:
        ans_discharged.append(0)
        ans_charged.append(0)
        discharge = 0
    if days_counter == 24:
        days_counter = 0
        charge = 0
        discharge = 0
    print(ans_discharged)
    print(ans_charged)

我的输出:

Charge = [0, 100, 190, 340, 0, 0, 0, 0, 0, 0, 40, 100, 0, 0]
Discharge = [0, 0, 0, 0, 190, 40, 0, 0, 0, 0, 0, 0, 0, 0]

我知道这是一个大问题,但有人可以帮忙回答吗,因为正如你所看到的,我得到了 40 和 100,而不是 50 和 110

最佳答案

您的代码非常复杂,可以简化:

signal = [1,1,1,1,0,0,0,0,0,1,1,1,0,0]
excess_energy = [0,100,90,160,20,0,0,0,0,0,50,60,70,0]
charge = []
discharge = []

current = 0
for i, value in enumerate(signal):
    if value == 0 : # Discharging
        current = max(current - 150, 0)
        discharge.append(current)
        charge.append(0)
    else:
        current = current + min(excess_energy[i], 150)
        charge.append(current)
        discharge.append(0)

print(charge)
print(discharge)

关于python - 如何关联两个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62038778/

相关文章:

python - 用于评估的 Tensorflow 平均绝对误差 (MAE)

python - 当子列表的大小依赖于数据值(种子和扩展)时,将列表拆分为子列表

python - 如何从 Python 3 脚本创建独立的可执行文件?

python - 从文本文件复制数据并将其插入 URL

list - 如何在 Julia 中存储循环结果

python - IPython 自定义制表符完成用户魔法功能

python - 列表列表更改意外地反射(reflect)在子列表中

python - 无效语法指向 python 中的 "return"

php - 在mysql列中存储列表

c# - C# 中列表的继承,以及在 FindAll() 之后的显式转换