mercredi 17 juin 2020

How to get values added in a list and not just the last added value?

I have a code that looks like this:

if len_after > len_before:
    # get resource demand of finish_act
    r_finish_act = [d for a, d in zip(j_set, R) if a in finish_act]

    # update available resources
    for i in r_finish_act:
        x = i[0]
    avail_res[0] = avail_res[0] + x

else:
    avail_res[0] = avail_res[0]

The objective of this code is to get the resource demand of the finish activity/ies (r_finish_act) and add them to avail_res[0]. The first thing to check is if an activity is added in finish_act. This is done using the if-statement. Afterwards, r_finish_act is obtained using j_set and R.

j_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
R = [[0, 0], [4, 3], [2, 0], [2, 1], [0, 3], [1, 1], [1, 2], [2, 1], [0, 3], [2, 2], [0, 0]]

I only need the first element of r_finish_act. Lastly, obtained r_finish_act is added to avail_res[0]. The problem is, with this code, if there are more than one activities added to finish_act. Only the r_finish_act of the last activity added in finish_act is added in avail_res[0].

For example:

finish_act_before = [1]
avail_res[0] = 1
finish_act_after = [1, 2, 3] 
r_finish_act = [[4, 3], [2, 0]] 

Output of the code above:

avail_res[0] = 1 + 2 = 3

Expected output:

avail_res[0] = 1 + 4 + 2 = 7

How could I make this change for the code above such that all r_finish_act of activities added in finish_act will be added to avail_res[0] and not just the last one? Note that activities to be finished vary. It can be just one activity, two, and so on.

Any help/suggestions would be appreciated! Thanks!

Aucun commentaire:

Enregistrer un commentaire