首页 > 解决方案 > 如何在 Python 中的 for 循环中创建不同的变量

问题描述

我正在制作一个具有 for 循环的程序,每次运行循环时,我都希望它创建一个新变量,例如

for item in range(0, size)
     (Make new variable bit1, bit2, bit3, bit4, etc with value of 0)

这可能吗?

标签: python

解决方案


创建一个List变量并像这样附加到它:

bits = []
for item in range(0, size)
    bits.append(0)

# now you have bits[0], bits[1], bits[2], etc, all set to 0

推荐阅读