将字符串设置为变量nam

2024-10-01 07:17:55 发布

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

Possible Duplicate:
How can I use a string with the same name of an object in Python to access the object itself?

我正在尝试根据匹配结果将名称更改为变量。。。在

inds_EUR = [whatever]
inds_AFR = [foo]
inds_ASN = [other]

pop=inds_EUR ##imagine this is the case
for pp in ('EUR', 'AFR', 'ASN'):
    if pp in pop:
    paap='inds_'+str(pp)        
    break
foos=eval(paap)

我要做的是将“foos”设置为要传递给这个表达式的列表

^{pr2}$

它可以工作,但不知道使用eval()表达式是否危险,这里使用vars()可能是这样 我做得对吗?在

提前谢谢你

佩西


Tags: theinobject表达式evaleurpoppp