将字符串设置为变量名称

pei*_*ixe 1 python string eval

可能重复:
如何在Python中使用与对象名称相同的字符串来访问对象本身?

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

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)
Run Code Online (Sandbox Code Playgroud)

我正在尝试的是将"foos"设置为传递给此表达式的列表

matches = [item for item in inds_vcf if item in foos]
Run Code Online (Sandbox Code Playgroud)

它工作,但不知道使用这个eval()表达式是否危险,这里可能是因为它使用vars()我是以正确的方式做到的吗?

提前致谢,

佩希

eum*_*iro 7

使用字典:

inds = {'EUR': [whatever],
        'AFR': [foo],
        'ASN': [other]}

foos = inds['EUR']
Run Code Online (Sandbox Code Playgroud)