I had some problems with the code that was given in an answer at this post: Can I use a nested for loop for an if-else statement with multiple conditions in python?
import pprint
board = {
'1a': 'bking',
'4e': 'bpawn',
'2c': 'bpawn',
'3f': 'bpawn',
'5h': 'bbishop',
'6d': 'wking',
'7f': 'wrook',
'2b': 'wqueen'
}
count = {}
for k, v in board.items():
count[k[0]][k[1:]] = v
pprint.pprint(count)
I wanted to get the following dictionary:
count = {'b': {'king': 1, 'pawn': 3, 'bishop': 1},
'w': {'king': 1, 'rook': 1, 'queen': 1}}
Received error:
Traceback (most recent call last):
File "/Users/Andrea_5K/Library/Mobile Documents/com~apple~CloudDocs/automateStuff2/ch5/flatToNest2.py", line 21, in <module>
count[k[0]][k[1:]] = v
KeyError: '1'