将字符串列表转换为表名

2024-06-08 15:46:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在这个“for”循环中生成表。为了让tablenames中的字符串成为表的名称,我需要做些什么。你知道吗

tablenames = ['t_10', 't_20', 't_30', 't_40', 't_50', 't_60', 't_70', 't_80', 't_90', 't_100']
firstrow = ['10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%']

for t, r, in zip(tablenames, firstrow):
    t = [[r, '']]    

这是我最后做的-我知道,它不漂亮(我知道有一种方法可以生成我想要的空间数…它只是不合作…而且,cond1和cond0列表很奇怪…)

t_10 = [['10%', '', '', '', '', '', '', '', '', '','','','']]
t_20 = [['20%', '', '', '', '', '', '', '', '', '','','','']]
t_30 = [['30%', '', '', '', '', '', '', '', '', '','','','']]
t_40 = [['40%', '', '', '', '', '', '', '', '', '','','','']]
t_50 = [['50%', '', '', '', '', '', '', '', '', '','','','']]
t_60 = [['60%', '', '', '', '', '', '', '', '', '','','','']]
t_70 = [['70%', '', '', '', '', '', '', '', '', '','','','']]
t_80 = [['80%', '', '', '', '', '', '', '', '', '','','','']]
t_90 = [['90%', '', '', '', '', '', '', '', '', '','','','']]
t_100 = [['100%', '', '', '', '', '', '', '', '', '','','','']]


tnames = [t_10, t_20, t_30, t_40, t_50, t_60, t_70, t_80, t_90, t_100]

cond1 = [conditions_list[0], conditions_list[2], conditions_list[4], conditions_list[6], conditions_list[8]]
cond0 = [conditions_list[1], conditions_list[3], conditions_list[5], conditions_list[7], conditions_list[9]]

for t, c1, c0 in zip(tnames,cond1,cond0):
    c1_results = process_exhaust(c1,1)
    c0_results = process_exhaust(c0, 0)    
    t.append(c1_results[0])
    t.append(c1_results[1])
    t.append(c1_results[2])
    t.append(c0_results[0])
    t.append(c1_results[3])
    t.append(c0_results[1])

Tags: inforzipprocessresultsconditionslistc1
1条回答
网友
1楼 · 发布于 2024-06-08 15:46:30

尝试以编程方式创建变量名通常是个坏主意。您可以使用exec来完成,但是更好的方法是使用字典。你知道吗

D = {t: [r,] for t, r in zip(tablenames, firstrow)}

现在您可以执行D["t_10"]以获得所需的输出。你知道吗

相关问题 更多 >