dimanche 3 mars 2019

Python: update parent in for-loop

I would like to update my parent id correctly.

My existing list of dictionaries

id prj_id   parent
1  808348   1
2  808352   2
3  808356   3
4  808361   4
5  808365   0
6  808370   0
7  808370   6
8  808370   7
9  808370   8

Now I want to get it, that my parents get the number of the parent project

My Code:

index = 0
last_prj_id = ""

for row in result_gantt:
    current_prj_id = row['prj_id']
    parent = row['parent']
    parent = index

    if current_prj_id == last_prj_id:
        parent = last_id
    else:
        parent = index

    last_prj_id = row['prj_id']
    last_id = row['id']

Result from the Code:

id prj_id   parent
1  808348   0
2  808352   0
3  808356   0
4  808361   0
5  808365   0
6  808370   0
7  808370   6
8  808370   7
9  808370   8

My desired result

id prj_id   parent
1  808348   0
2  808352   0
3  808356   0
4  808361   0
5  808365   0
6  808370   0
7  808370   6
8  808370   6
9  808370   6

I do not know how to continue. Would appreciate your help

Aucun commentaire:

Enregistrer un commentaire