Problem Description
Want to generate several lists, the list name contains the loop variable, and the list content also contains the loop variable. E.g:
list_1 = [1]
list_2 = [1,2]
list_3 = [1,2,3]
...
list_i = [1,2,3,...,i]
solution:
code show as below:
names = locals()
for i in range(0, 10):
names[f'list_{
i}'] = []
for j in range(0,10):
if i >=j:
names[f'list_{
i}'].append(f'{
j}')
print(f'list_{
i}')
print(names[f'list_{
i}'])
The results are as follows:
Actual combat:
w_local = copy.deepcopy(weight_local[0])
names = locals()
for j in range(0, args.nBlocks - 1):
names[f'param_index_{
j}'] = [k for k in w_local.keys() if k.split('.')[1] == f'{
j}']
print(names[f'param_index_{
j}'])
Here's the result:
perfect!