0

I wonder if there is a way to generate new variables and its assaignment based on a particular global variable.

I have the following code which belongs to a larger code function. In this particular case:

VNF_TYPES = 2 

# Sum up vnf event per type in transitioned state
  total_vnf = [0]*VNF_TYPES 
  for event in states[key][0]:
    total_vnf[0] += event[0]
    total_vnf[1] += event[1]

If I change the value of:

VNF_TYPES = 4

Is there a way to automatically generate new variables and its assaignments in such a way that it results in:

# Sum up vnf event per type in transitioned state
  total_vnf = [0]*VNF_TYPES 
  for event in states[key][0]:
    total_vnf[0] += event[0]
    total_vnf[1] += event[1]
    total_vnf[2] += event[2] # automatically generated
    total_vnf[3] += event[3] # automatically generated
krm76
  • 357
  • 3
  • 13

2 Answers2

1

.... would it work ? I don't have your entire code so not sure what will end up with:

VNF_TYPE = 4
#Sum up vnf event per type in transitioned state
# total_vnf = [0]*VNF_TYPES 
for event in states[key][0]:
    for i in range(VNF_TYPE):
        total_vnf[i] += event[i]

give us a minimal reproducible example to work with

pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • Is bit difficult for me to give you a minimal reproducible example since the code lines that I wrote on the post belong to a function that at the same time it feeds from other functions. Nevertheless, your suggestion has helped me to achieve what I was trying, so I give it as an answer. Thx. – krm76 Mar 18 '21 at 16:14
0

Try it.

  total_vnf = [0]*VNF_TYPES 
  for event in states[key][0]:
      for pos in range(VNF_TYPES):
        total_vnf[pos] += event[pos]
Nayan Biswas
  • 112
  • 1
  • 4