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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
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.
 
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]?