Python将字符串转变为变量名

需求是

有一个列表 lst = [“A”, “B”, "C“],需要将得到三个以 A B C为变量名的列表,即A = [“A”] ,B=[“B”],C=[“C”]。

解决方法

lst = ["A", "B", "C“]
for i in lst:
	globals()[i] = [i]

print(A,B,C)

结果为
[‘A’] [‘B’] [‘C’]

具体参考:
https://www.jianshu.com/p/e774a42ce76c

关于globals和locals的用法参考:
https://www.cnblogs.com/milesma/p/12025811.html

你可能感兴趣的:(Python将字符串转变为变量名)