0

有人知道我在下面的代码中做错了什么吗?

ZeroList = ["53", "52", "24", "26", "36", "37", "40", "41", "43", "44", "45"]

for ZeroID in ZeroList:
    if row[ZeroID]:
        R+str(ZeroID) = float(row[ZeroID])
    else:
        R+ZeroID = 0

执行以下输出:

print R53
print R52
print R24

会输出:

2392.118329
232.298323
142.521513

我尝试了几个选项来尝试创建动态字符串,在上面的示例中,我尝试了两种方法。

有任何想法吗?

编辑

        ZeroList = {"R53": "", "R52": "", "R24": "", "R26": "", "R36": "", "R37": "", "R40": "", "R41": "", "R43": "", "R44": "", "R45": ""}

        for ZeroID in ZeroList:
            if row[int(ZeroID[-2:])]:
                ZeroList[ZeroID] = float(row[int(ZeroID[-2:])])
            else:
                ZeroList[RZeroID] = 0

        datatoapp = [row[0], row[1], row[8], row[9], '{0:0.2f}'.format(float(ZeroList[R52]))]
4

1 回答 1

5

将数据保留在变量名之外。

使用字典来存储您的项目,而不是创建动态键:

results = {}

for ZeroID in ZeroList:
    if row[ZeroID]:
        results[R+str(ZeroID)] = float(row[ZeroID])
    else:
        results[R+str(ZeroID)] = 0
于 2013-10-31T16:06:51.523 回答