Skip to main content
 首页 » 编程设计

python之使用变量访问另一个变量

2025年05月04日14EasonJim

所以我不确定如何描述它或用谷歌搜索它。我想要的是使用一个变量来访问另一个变量。我猜答案会使用各种字典。

personPaul = [1,2] 
personJoe = [3,4] 
 
def myFunc(name): 
    return person + name 
 
print myFunc("Paul") 
print myFunc("Joe") 

我想要的输出是

[1,2] 
[3,4] 

请您参考如下方法:

I'm guessing the answer will be using a dictionary of sorts

完全正确:-)

people = { 
    "Paul": [1,2], 
    "Joe": [3,4] 
} 
 
def myFunc(name): 
    return people[name] 
 
print myFunc("Paul") 
print myFunc("Joe") 

当然也可以去掉myFunc中间人,直接做print people["Paul"]。