Home > Software design >  How creating a list of variables that can adjust numbers of variables depends on length of values li
How creating a list of variables that can adjust numbers of variables depends on length of values li

Time:05-02

I want to make a list of variables that is always equal to the length of the list of values.

**Example**

list1 = [0,1,2]
return
[a0,a1,a2]

a0 = 0, a1 = 1, a2 = 2

list2 = [10,20,30,40]
return
[b0,b1,b2,b3]

b0 = 10, b1 = 20, b2 = 30, b3 = 40

CodePudding user response:

Well, you can do this, but I'm not sure I'd recommend it:

list1 = [0,1,2]
for i, v in enumerate(list1):
    exec(f"a{i} = {v}")
print(dir())
print([a0,a1,a2])

Output:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a0', 'a1', 'a2', 'i', 'list1', 'v']
[0, 1, 2]

The repeated calls to the exec() function have added a0, a1 and a2 to the current local scope as shown by the output of the call to dir(). Printing [a0, a1, a2] confirms that these variables have the values in list1.

As @ user2736738 has suggested in a comment, your question seems to indicate that the root problem you are trying to solve may be better addressed by a different approach.

UPDATE: Here's some additional code to further illustrate how the variables and the list of variables will behave upon subsequent updates while also addressing OP's query in a comment, namely "I want the output to be a list of variables ... how to create the list while not type it manually?":

list1 = [0,1,2]
for i, v in enumerate(list1):
    exec(f"a{i} = {v}")
exec(f"new_list = [{','.join('a' str(i) for i in range(len(list1)))}]")
print(dir())

print("new_list:")
print(new_list)

print(','.join('a' str(i) for i in range(len(list1)))   ':')
exec(f"print({','.join('a' str(i) for i in range(len(list1)))})")

if len(new_list) > 0:
    a0 = 99
    print(f"new_list after updating a0 to {a0}:")
    print(new_list)

if len(new_list) > 1:
    new_list[1] = 66
    print(f"new_list after updating element at index 1 to {new_list[1]}:")
    print(new_list)
    print(','.join('a' str(i) for i in range(len(list1)))   f' after updating element at index 1 to {new_list[1]}:')
    exec(f"print({','.join('a' str(i) for i in range(len(list1)))})")

Output:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a0', 'a1', 'a2', 'i', 'list1', 'new_list', 'v']
new_list:
[0, 1, 2]
a0,a1,a2:
0 1 2
new_list after updating a0 to 99:
[0, 1, 2]
new_list after updating element at index 1 to 66:
[0, 66, 2]
a0,a1,a2 after updating element at index 1 to 66:
99 1 2

The second exec() call starting exec(f"new_list = ... will do what you asked in your comment. However, I would again urge you to consider whether this is truly helpful to you in your larger goals, and alert you to the disconnect between this list of variables and the variables themselves.

As you can see, assigning a new value to the variable a0 has no effect on new_list, the variable referring to the list [a0, a1, a2], and assigning a new value to new_list[1], the element at index 1 within that list, has no effect on the variable a1 which was used to initialize that list.

  • Related