I'm trying to compare two dicts:
> x_state
[{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}]
> objects
[{'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}, {'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}]
To find differences easier I sort them first and then run some compare-methods over it. As most of the similar problems on SO result in a difference in the length or issues in reading of the file (x_state is a JSON from a file) I decided to count them:
unos=[sorted(l, key=itemgetter("ID"))for l in (x_state,objects)]
> unos
[[{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}],
[{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}, {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}, {'ID': 4, 'Name': 'D'}, {'ID': 5, 'Name': 'X'}]]
#Looks like nothing weird happened
> len(unos)
2
> len(unos[0])
4
> len(unos[1])
4
Seems fine, yet from that point everything just fails:
> for i,j in unos: print(f"{i}------{j}")
*** ValueError: too many values to unpack (expected 2)
> for i,j in zip(unos): print(f"{i}------{j}")
*** ValueError: not enough values to unpack (expected 2, got 1)
Trying something else to see whether or not both contain the same data:
> any(x != y for x, y in unos)
*** ValueError: too many values to unpack (expected 2)
What is going on here? And how would I properly debug this issue?
CodePudding user response:
The sort command was executed wrongly. Correct:
ipdb> !for x,y in zip(sorted(x_state, key=itemgetter("ID")),sorted(objects, key=itemgetter("ID"))): print(x,y)
{'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'} {'ID': 1, 'Wert': '6,6743', 'Name': 'Δ'}
{'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'} {'Wert': 'Δ', 'ID': 3, 'Name': 'Δ'}
{'ID': 4, 'Name': 'D'} {'ID': 4, 'Name': 'D'}
{'ID': 5, 'Name': 'X'} {'ID': 5, 'Name': 'X'}