逐项平衡2组数值

Balance 2 sets of values item by item

我的问题可能看起来与其他问题相似,但我还没有找到任何与我需要的相差甚远的问题(我仍在四处寻找以防万一)。当我发现自己有一个有限的负值列表和另外 2 个有限的正值集(浮点数)时,我有一个项目(不需要项目的详细信息)。以下面的示例集为例:

negative_values = [-0.246497, -0.341068]
positive_values_1 = [0.522148, 0.923764, 0.112573, 0.401668]
positive_values_2 = [0.281474]

这些集合的大小不等(空到大小 N)。我需要做的是获取第一个正集合中的值并尝试(根据集合可能无法实现)通过逐个值将负集合中的值相加来使负集合中的值变为 0。

如果只用第一个不行,那就用第二个,如果还是不行,就把'negative_values'中的值尽量设为0。

使用上面的设置,这将呈现如下内容:

def cancelOut(negative_values, positive_values_1, positive_values_2):
    # Algorith doing something

new_negative_values = [0, 0]
new_positive_values_1 = [0, 0.858347, 0.112573, 0.401668]
new_positive_values_2 = [0.281474]

发生的事情是 positive_values_1 中的第一个值使 negative_value 中的第一个值变为 0,并将第二个值增加到更接近 0。然后 [=27= 中的第二个值的一部分] 将 negative_value 中的第二个值设为 0 然后算法就完成了。我只是想将正集中的值一个一个地添加到 negative_value 中的值,直到它们都为 0 或者直到 positive_value 集都为 0.

我不知道是否有简单的方法可以做到这一点,或者我是否需要专门逐个遍历每个设置值并计算出我想要的。

先谢谢大家了!

这是一个开始的地方 - 这将是一个逻辑练习和跟踪事物。

a = negative_values
b = positive_values_1
c = positive_values_2

ab 的第一项开始。

add the a and b item together  
   if the result is > 0 change a's item to 0  
      and get a's next item to use in the next iteration  
          use the result for the b item in the next iteration
   if the result is 0 change a and b's item to 0  
       and get the next items from a and b to use in the next iteration  
   if the result is < 0 change b's item to zero  
       and get b's next item for use in the next iteration
           use the result for the a item in the next iteration   
if you get a StopIteration for a you are done
    make the current item/index of b (or c) = the result
if you get a StopIteration for b, switch to c
if you get a StopIteration for c you are done
    make the current item/index of a = the result
repeat