Comp Sci Reverse Dictionary Keys: d={1:7,2:5,3:7,4:16,5:25,6:5,9:7}

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
AI Thread Summary
The discussion focuses on reversing the keys of a dictionary where multiple keys can have the same value. The initial code correctly reverses some keys, but fails to capture the last key for duplicate values, specifically at the value 7. Participants highlight that the issue arises from the way the `get()` method is used, which only retains the last key associated with duplicate values. The output should reflect all keys correctly, such as having 7:3 instead of 7:1. Clarification is sought on how to properly update the dictionary during each iteration to resolve the issue.
ver_mathstats
Messages
258
Reaction score
21
Homework Statement
Given a dictionary d, create a new dictionary that reverses the keys and values of d. Thus, the keys of d become the values of the new dictionary and the values of d become the keys of the new dictionary. If there is a common value for several keys in d then only the last key becomes a value in the new dictionary. Perform the following test case: when d is {1:7, 2:5, 3:7, 4:16, 5:25, 6:5, 9:7} your code should produce the dictionary {7:9, 5:6, 16:4, 25:5}.
Relevant Equations
python
Python:
d={1:7,2:5,3:7,4:16,5:25,6:5,9:7}
reverse_d={}
for a, b in d.items():
    reverse_d[b] = reverse_d.get(b,a)
print(reverse_d)

I ended up reversing the keys in the dictionary, so I have the 7, 5, 16, and 25 correct. I am just struggling with making the last key become a new value in the dictionary. Overall my code prints {7:1, 5:2, 16:4, 25:5}, I just do not know how to get the last element. Could I please have help with that? Thank you.
 
Physics news on Phys.org
ver_mathstats said:
my code prints {7:1, 5:2, 16:4, 25:5}
Maybe I'm misunderstanding but it looks like the bug is occurring earlier than 9:7. It's occurring at 3:7.

"If there is a common value for several keys in d then only the last key becomes a value in the new dictionary. "

Your output should at least be 7:3, not 7:1.

I am not sure how to fix it but I suspect that you need to create a new, updated d each iteration. I think, currently, you're always operating on the initial d.
 
Python:
    reverse_d[b] = reverse_d.get(b,a)
What does this do if the key b already exists? What do you want it to do?

Think simpler (I can't help thinking that you have copied the get() from the answer to a different and harder problem).
 
pbuk said:
Python:
    reverse_d[b] = reverse_d.get(b,a)
What does this do if the key b already exists? What do you want it to do?

Think simpler (I can't help thinking that you have copied the get() from the answer to a different and harder problem).
https://www.w3schools.com/python/python_dictionaries.asp here's the website we were given to use so I thought I would use the get().
 
DaveC426913 said:
Maybe I'm misunderstanding but it looks like the bug is occurring earlier than 9:7. It's occurring at 3:7.

"If there is a common value for several keys in d then only the last key becomes a value in the new dictionary. "

Your output should at least be 7:3, not 7:1.

I am not sure how to fix it but I suspect that you need to create a new, updated d each iteration. I think, currently, you're always operating on the initial d.
Yes I see, that makes sense. Thank you.
 
ver_mathstats said:
Yes I see, that makes sense. Thank you.
No, that is not the answer, you just need to follow through your code and work out what it does when it gets to 3:7.
 
ver_mathstats said:
No, that is not the answer, you just need to follow through your code and work out what it does when it gets to 3:7.
ver_mathstats said:
...so I thought I would use the get().
But at 3:7, do you want to know the current value of reverse_d[7]?
 

Similar threads

Replies
2
Views
2K
Replies
5
Views
3K
Replies
7
Views
1K
Replies
1
Views
2K
2
Replies
67
Views
14K
Replies
1
Views
12K
Replies
3
Views
3K
Replies
2
Views
2K
Back
Top