python - 在 python 的 for 循环中创建唯一名称列表

标签 python list for-loop unique

我想在 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/

相关文章:

javascript - 使用对象属性在 for 循环中声明变量

c++ - 使用 "for"打破 "break"循环被认为是有害的?

python - 在 python 中使用带有 fab 的子进程

python为列表中的每个元素添加一个id

c++ - std::list::splice 和其他列表容器的复杂性

计算每个字符在 C 中出现的次数

python - 取决于一个参数的不确定循环次数

python - Telegram 对话机器人中后退按钮实现的最佳实践

Python 结构库附加不需要的字符 'L'

python - 从两个for循环python获取x,y值