gpt4 book ai didi

python - 如何关联两个列表?

转载 作者:行者123 更新时间:2023-12-04 09:49:12 24 4
gpt4 key购买 nike

我有两个列表和一些参数。在这些输入的帮助下,我正在为我的电池充电和放电。在第一个列表中,有 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 且 extra_energy 大于 0 时才能充电。
  • 如果 extra_energy 大于 150MW,则电池将仅以 150MW 充电,或者如果它小于 150MW,则它可以使用任何多余能量值充电。
  • 电池只能在信号为 0 时放电,固定时间为 150MW。
  • 电池不能低于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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com