0

Anyone know what I'm doing wrong in the code below?

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

Doing the following output:

print R53
print R52
print R24

Would output:

2392.118329
232.298323
142.521513

I've tried several options to try and create the dynamic string, in the above example there are two ways that I've tried.

Any ideas?

EDIT

        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]))]
3
  • 1
    What is R and what is your desired output? Commented Oct 31, 2013 at 16:02
  • 2
    Are you trying to create dynamic variables? If so, don't. Use a dictionary instead. Commented Oct 31, 2013 at 16:05
  • R+str(ZeroID) = float(row[ZeroID]) this part is wrong. there shouldn't be sentence left to equal sign. Commented Oct 31, 2013 at 16:06

1 Answer 1

5

Keep your data out of your variable names.

Use a dictionary to store your items, creating dynamic keys instead:

results = {}

for ZeroID in ZeroList:
    if row[ZeroID]:
        results[R+str(ZeroID)] = float(row[ZeroID])
    else:
        results[R+str(ZeroID)] = 0
Sign up to request clarification or add additional context in comments.

3 Comments

how am I supposed to call my data later on? See my edit for my edits
Why not just refer to the row indices directly? row[ZeroID] = float(row[Zeroid]), then print row[53] later on.
@Hyflex: Then test for that; test the length of the row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.