test = {'key1':'val1','key2':100,'anotherkey':'val3'}
我该如何以对键的数量或名称不敏感的方式提取名为key1,key2和anotherkey的变量以及上面的相应值?
我尝试了涉及map()和.get()的解决方案,但在寻找特定键时它们似乎可以工作
答案 0 :(得分:-1)
尝试一下
test = {'key1':'val1','key2':100,'anotherkey':'val3'}
for key,val in test.items():
exec(key + '=val')
print(key1)
# val1
print(key2)
# 100
print(anotherkey)
# val3