The weird error of python list generation

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"

< hr >

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?

Jun.27,2022

in most cases, calling locals () or globals () to store variables will cause more trouble, and it is not the correct operation variable (reasonable?) Way.

Why not use list or map to store content?
take map as an example

def initialize_my_list():
    myVars = {}
    for i in range(0,2):
        for j in range(0,2):
            myVars['V'+'%s'%i+'%s'%j]=('somedata')

    T0=[]
    T0.append([myVars['V%s%s'%(i,j)] for i in range(0,2) for j in range(0,2)])

buddy, you seem to have forgotten the function of local () function

.

clipboard.png

The locals () function returns all local variables of current position as a dictionary type, and each function can be seen as a local scope .

-sharp `local()`, `local()`:
T0.append([locals()['V%s%s'%(i,j)] for i in range(0,2) for j in range(0,2)])
-sharp `local()`append()

you can set an intermediate variable

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=[]
    locals_dict = locals()
    T0.append([(locals_dict['V%s%s'%(i,j)]) for i in range(0,2) for j in range(0,2)])
    -sharp T0.append(locals()['V00'])
    -sharpV00T0
    print(locals_dict)
initialize_my_list()

like this, it's done

. < hr >

as for the second section of code, local () does return local variables in scope initialize_my_list () , so it is feasible

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf5aeb-317eb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf5aeb-317eb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?