0

I'm trying to insert values in a list using the name of a variable dynamically but I am not being able to do it.

lst_prio = [90, 100]
p_90 = [0.11, 0.2, 0.15, 0.6, 0.9]
p_100 = [0.1, 0.3, 0.5, 0.7, 0.8]
lst_crv = [4,3,2,5,6]
crv = [1,2,3,4,5]

lst_percs = []
for x in range(len(lst_prio)):
    lst_percs.append("lst_p_"+str(lst_prio[x]))

dic =dict(zip(lst_prio,lst_percs))

for w in range(len(lst_prio)):
    dic[lst_prio[w]] =[]
    for i in range(len(crv)):
        for j in range(len(lst_crv)):
            if crv[i] == lst_crv[j]:
#Below I would like to insert the list as the append value (p_90 to p_100) dynamically based on the dictionary I've created 
                dic[lst_prio[w]].append(p_90[i])

The result I am getting (because I am not being able to iterate):

lst_p_90 = [0.2, 0.15, 0.6, 0.9]

lst_p_90 = [0.2, 0.15, 0.6, 0.9]

The result I would like:

lst_p_90 = [0.2, 0.15, 0.6, 0.9]

lst_p_100 = [0.3, 0.5, 0.7, 0.8]
bruni
  • 13
  • 3
  • 1
    Use one dict instead of two separate list variables. `p = {90: [0.11, ...], 100: [0.1, ...]}`. – chepner Jun 13 '19 at 20:10
  • 1
    I do not see any modification of lst_p_90 or lst_p_100 in your code so I do not understand how would you get your current and expected results. The current result you present is simply false. – CodeSamurai777 Jun 13 '19 at 20:18
  • @mrangry777 I mistyped the list name. – bruni Jun 14 '19 at 02:28

1 Answers1

0

I think I get what you meant. You want to dynamically evaluate the variable based on the name. Try this for the last lines.

    if crv[i] == lst_crv[j]:
        number = lst_prio[w]
        p_n = locals()['p_' + str(number)]
        dic[number].append(p_n[i])

Related: How to get the value of a variable given its name in a string?

digit plumber
  • 1,140
  • 2
  • 14
  • 27