Skip to main content
 首页 » 编程设计

python之将字符串设置为变量名

2025年05月04日20shihaiming

<分区>

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”设置为要传递给此表达式的列表

matches = [item for item in inds_vcf if item in foos] 

它有效,但不知道使用这个 eval() 表达式是否危险,如果使用 vars() 可能会危险 我的做法是否正确?

提前致谢

画画