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

  • Context: Comp Sci 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around reversing the keys and values in a Python dictionary, specifically addressing issues encountered in the implementation of this reversal. Participants are seeking assistance with a coding problem related to handling duplicate values in the original dictionary.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet for reversing a dictionary and notes that the output does not include all expected values, specifically missing the last element.
  • Another participant points out that the issue may arise from how the code handles duplicate values, suggesting that only the last key is retained in the new dictionary for common values.
  • Several participants question the logic behind the use of the `get()` method in the code, asking what should happen if the key already exists in the reversed dictionary.
  • There is a suggestion that the participant may need to create a new, updated dictionary during each iteration to avoid issues with the initial dictionary.
  • One participant expresses uncertainty about the behavior of the code at a specific point (3:7) and encourages others to trace through the code to understand its operation.

Areas of Agreement / Disagreement

Participants express differing views on the handling of duplicate values and the effectiveness of the current code. There is no consensus on the correct approach to resolve the issue, and the discussion remains unresolved.

Contextual Notes

Participants highlight the need to consider how the code behaves with duplicate values and the implications of using the `get()` method in this context. There are unresolved questions about the logic and flow of the code, particularly at specific points in the iteration.

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 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
2K
  • · Replies 67 ·
3
Replies
67
Views
16K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K