novice to Python, just studied for a month. Now we need to manage a pile of data. We want to generate variable names in an intuitive form to facilitate subsequent data processing, such as V00MagneT01, etc., and then use the following code to report an error in any case, asking for an answer
def initialize_my_list():
""""""
for i in range(0,2):
for j in range(0,2):
locals()["V"+"%s"%i+"%s"%j]=("somedata")
print(locals())-sharpV00V01V10print
T0=[]
T0.append([locals()["V%s%s"%(i,j)] for i in range(0,2) for j in range(0,2)])
-sharpV00T0
however, an error is reported as soon as it is run:
Traceback (most recent call last):
File "deleteok.py", line 24, in < module >
initialize_my_list ()
File "deleteok.py", line 22, in initialize_my_list
T0.append ([locals () ["V% s% s% s percentage percent) for i in range (0for i in range 2) for j in range (0for i in range 2)])
File "deleteok.py", line 22, in < listcomp >
T0.append ([locals () ["V% s% s% percent percent) (I) J)] for i in range (0jue 2) for j in range (0jue 2)])
KeyError: "V00"
the weird thing is that if you hit it by hand, you don"t have to generate the list variable name automatically. The code is as follows:
def initialize_my_list():
""""""
V00=(120,123,88)
V01=(454,-12,234)
V02=(67,345,852)
V10=(44,12,33)
V11=(1234,5634,97)
V12=(9348,12,45)
globals()["T0"]=[locals()["V"+"01"],V01,V02]-sharp
print(T0)
initialize_my_list()
Why is this?