在 Python (2.7)中有更简单的方法吗?注意: 这不是什么花哨的东西,就像把所有的局部变量放入一个字典。只有我在列表中指定的那些。
apple = 1
banana = 'f'
carrot = 3
fruitdict = {}
# I want to set the key equal to variable name, and value equal to variable value
# is there a more Pythonic way to get {'apple': 1, 'banana': 'f', 'carrot': 3}?
for x in [apple, banana, carrot]:
fruitdict[x] = x # (Won't work)