创建包含特定模式的所有变量的列表

时间:2018-06-22 12:17:38

标签: python list variables

我正在尝试创建所有varN变量的列表,其中N是一个数字。假设我有6个varN变量,并且我想创建一个列表vars_lst,其中包含属于varN的所有值:

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

我试图通过for循环来实现:

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)

但是问题在于列表包含字符串var1-var6,而不包含变量值。

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

我希望它包含:

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

[编辑] 这实际上是我的脚本:

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

所以我想做的就是将所有这些vmaf_lines插入日志文件

0 个答案:

没有答案