0

I am trying to create a list of all varN variables , where N is a number. Let's say I have 6 varN variables and I want to create a list vars_lst containing all the values belonging to the varN:

vars_lst = [var0, var1, var2, var3, var4, var5]

I have tried to do it with a for loop:

var0 = 10
var1 = 20
var2 = 30
var3 = 40
var4 = 50
var5 = 60
vars_lst = []
for i in range(6):
    vars_lst.append("var" + str(i))
print(vars_lst)

But the problem is that the list contains the strings var1 - var6, not the variables values.

['var0', 'var1', 'var2', 'var3', 'var4', 'var5']

And I want it to contain:

[10, 20, 30, 40, 50, 60]

[EDIT] This is actually my script:

import os, subprocess

vmaf_line1 = "VMAF STATISTICS for " + inp_file + ":"
vmaf_line2 = "========================="
vmaf_line3 = "PSNR Average:       " + str(psnr_avg) + "dB"
vmaf_line4 = "PSNR Minimal:       " + str(psnr_min) + "dB"
vmaf_line5 = "PSNR Maximal:       " + str(psnr_max) + "dB"
vmaf_line6 = "========================="
vmaf_line7 = "SSIM Average:       " + str(ssim_avg)
vmaf_line8 = "SSIM Minimal:       " + str(ssim_min)
vmaf_line9 = "SSIM Maximal:       " + str(ssim_max)
vmaf_line10 = "========================="
vmaf_line11 = "MS-SSIM Average:    " + str(ms_ssim_avg)
vmaf_line12 = "MS-SSIM Minimal:    " + str(ms_ssim_min)
vmaf_line13 = "MS-SSIM Maximal:    " + str(ms_ssim_max)
vmaf_line14 = "========================="
vmaf_line15 = "VMAF Average:       " + str(vmaf_avg)
vmaf_line16 = "VMAF Minimal:       " + str(vmaf_min)
vmaf_line17 = "VMAF Maximal:       " + str(vmaf_max)
vmaf_line18 = "========================="
vmaf_lines = []
for i in range(1, 19):
    vmaf_lines.append(locals()["vmaf_line" + str(i)])
write_file(vmaf_lines, log)

def write_file(lines_list, log_file):
    if not os.path.isfile(log_file):
        subprocess.call(["touch", log_file])
    line_num = 1
    for i in range(len(lines_list)):
        line_content = str(line_num) + "i" + lines_list[i]
        subprocess.call(["sed", "-i", line_content, log_file])
        line_num +=1
    return None

So what I am trying to do is to insert all these vmaf_lines into a log file

Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26
  • 3
    don't create variables named that way for starters. – Jean-François Fabre Jun 22 '18 at 12:19
  • 1
    Why not just create a list holding `range(10, 70, 10)`, then iterate that? Dynamic variables are almost never the right way. – Carcigenicate Jun 22 '18 at 12:19
  • 1
    `vars_lst.append(locals()["var" + str(i)])` or `vars_lst.append(globals()["var" + str(i)])`. Yeah that sucks right? then don't do that. – Jean-François Fabre Jun 22 '18 at 12:20
  • the solution of @Jean-FrançoisFabre works perfectly, thanks – Georgi Stoyanov Jun 22 '18 at 12:22
  • 1
    @GeorgеStoyanov No, don't use that. He was half-joking. Except for the extreme corner case, you should never be dynamically accessing variables like that. It generally leads to harder to understand, harder to debug code. Just use a list to have variable number of variables based on numbers like you have here, or use a dictionary to have a variable number based on arbitrary names. The solution he showed uses a dictionary anyways, but it contains all variables contrained in that scope, and you run the risk of name collisions which would be a nightmare to debug. – Carcigenicate Jun 22 '18 at 12:29
  • @GeorgеStoyanov In the your example here, just have `var_lst = range(10, 70, 10)`, then to get "`var2`", use `var_lst[2]`. – Carcigenicate Jun 22 '18 at 12:32
  • @Carcigenicate thanks for the hint, the problem is that `var0` to `var5` are using different values, not 10 - 60, it is more complicated, I have 26 variables which I want to put into a list and they are strings but I don't want to write `vars_lst = [var0, var1, var2, ..., var25]` – Georgi Stoyanov Jun 22 '18 at 13:10
  • 1
    @GeorgеStoyanov The problem then is that you've stuck the data in a ton of loose variables. If you have a lot of data, it should be put in a structure like a list from the start. As soon as you start writing `var1`, `var2`..., you've almost definitely set up your data wrong and should rethink your design. – Carcigenicate Jun 22 '18 at 15:05
  • @Carcigenicate, I have added a part of my original script, what I am trying to do is to write all these lines to a file without too much of repeating myself. – Georgi Stoyanov Jun 22 '18 at 15:27
  • 1
    @GeorgеStoyanov Why not just have a `lines` list, and put each string in the list? That would do away with the need for the first `for` loop that uses `locals`. I'm at work right now, but I could show how this could be written neater when I get off. Or, post this as a complete example on Code Review, and I'm sure there would be many people willing to help. – Carcigenicate Jun 22 '18 at 15:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173641/discussion-between-georg-stoyanov-and-carcigenicate). – Georgi Stoyanov Jun 22 '18 at 15:41

0 Answers0