I'm working on a school assignment that asks me to:
1)Take my previously created dictionary and write it to a file as a string.
2)Then import that dictionary to python again and invert it
3)Write the inverted dictionary to a new file.
I asked a question about this program previously and got a great answer but I'm still having a rough time getting through an error. When I try running the program I get a Type error: unhashable type: 'list'. I am assuming it has to do with the invert function.
When I run that function in a separate script using the ChessPlayerProfile dictionary it seems to work fine. But when I try to use it in this script through the return dictionary from Read_Invert_Write function's output I get the error.
Any ideas what I am doing wrong? Any help would be appreciated. Here is the code and output:
import os
import json
ChessPlayerProfile = {
"Matt": [("Rating: ", 2200), ("FIDE ID: 0147632DF"), ("Member Status: ", True)],
"Will": [("Rating: ", 2200), ("FIDE ID: 3650298MK"), ("Member Status: ", False)],
"Jithu": [("Rating: ", 1900), ("FIDE ID: 5957200LH"), ("Member Status: ", True)],
"Lisa": [("Rating: ", 2300), ("FIDE ID: 7719328CX"), ("Member Status: ", False)],
"Nelson": [("Rating: ", 2500), ("FIDE ID: 6499012XX"), ("Member Status: ", True)],
"Miles": [("Rating: ", 1600), ("FIDE ID: 4392251TJ"), ("Member Status: ", True)],
}
def write2file():
with open("chessdict.txt", "w+") as f:
json.dump(ChessPlayerProfile, f)
def Read_Invert_Write():
with open("chessdict.txt", "r") as f:
Content = (json.load(f))
Content = dict(Content)
invert(Content)
with open("newoutput.txt", "w+") as f:
json.dump(Content, f)
def invert(d):
inverse = dict()
for key in d:
val = d[key]
for item in val:
if item not in inverse:
inverse[item] = [key]
else:
inverse[item].append(key)
print(inverse)
return inverse
def main():
write2file()
Read_Invert_Write()
main()
Output:
Traceback (most recent call last):
File "/home/vigz/PycharmProjects/pythonProject/copytest2.py", line 46, in
main()
File "/home/vigz/PycharmProjects/pythonProject/copytest2.py", line 45, in main
Read_Invert_Write()
File "/home/vigz/PycharmProjects/pythonProject/copytest2.py", line 24, in Read_Invert_Write
invert(Content)
File "/home/vigz/PycharmProjects/pythonProject/copytest2.py", line 35, in invert
if item not in inverse:
TypeError: unhashable type: 'list'