我想在 for 循环中创建一系列具有唯一名称的列表,并使用索引创建列表名称。这是我想做的
x = [100,2,300,4,75]
for i in x:
list_i=[]
我想创建空列表,例如
lst_100 = [], lst_2 =[] lst_300 = []..
有什么帮助吗?
最佳答案
不要动态命名变量。这使得与他们一起编程变得困难。相反,使用字典:
x = [100,2,300,4,75]
dct = {}
for i in x:
dct['lst_%s' % i] = []
print(dct)
# {'lst_300': [], 'lst_75': [], 'lst_100': [], 'lst_2': [], 'lst_4': []}
关于python - 在 python 的 for 循环中创建唯一名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14819849/