Python Forum
Can a dictionary key be an integer?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a dictionary key be an integer?
#7
Thanks for your trouble, I appreciate it! I am still tinkering with this and it is STILL not working.

Attached is a screen shot of the output file. You can see that row 4, column 2 is the first student number. I tried the insert the values in column 8, then column 12. I always save as a copy, just to be careful.

   

This is from the interactive shell:

Quote:>>> activeSheetwb2.cell(row=4, column=2).value
'1625010401'
>>> StudentNumAndScore[1625010401]
16
>>> key = 1625010401
>>> value = StudentNumAndScore.get(key,'nix')
>>> value
16

The key is correct, starting at row 4, column 2 and the value is correct, 16:

for rowNum in range(4, maxRow2 + 1): 
    key = activeSheetwb2.cell(row=rowNum, column=2).value
    value = StudentNumAndScore.get(key, 'nix')
    activeSheetwb2.cell(row=rowNum, column=12, value=value)
If a key is not found, I set the value 'nix', meaning 'nothing'. From the screenshot, you can see, all I get is 'nix'.

Can anyone see what the problem is? Why do I not get the 'value' in the correct row and column?

EDIT: I finally answered my own question! If I write int(key) it works!!

for rowNum in range(4, 10):
	    key = activeSheetwb2.cell(row=rowNum, column=2).value
	    value = StudentNumAndScore.get(int(key), 'nix')
	    print(key, value)

Quote:1625010401 16
1625010402 15
1625010404 16
1625010405 17
1625010406 15
1625010407 18
Reply


Messages In This Thread
Can a dictionary key be an integer? - by Pedroski55 - Sep-27-2017, 05:20 AM
RE: Can a dictionary key be an integer? - by buran - Sep-27-2017, 05:53 AM
RE: Can a dictionary key be an integer? - by buran - Sep-28-2017, 01:29 PM
RE: Can a dictionary key be an integer? - by buran - Sep-30-2017, 02:23 PM
RE: Can a dictionary key be an integer? - by Pedroski55 - Oct-04-2017, 11:50 PM
RE: Can a dictionary key be an integer? - by buran - Oct-05-2017, 05:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing an integer by looking at a dictionary wendysling 3 2,338 May-02-2019, 03:38 PM
Last Post: wendysling

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020