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

  • Comp Sci
  • Thread starter ver_mathstats
  • Start date
In summary: 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.
  • #1
ver_mathstats
260
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
  • #2
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.
 
  • #3
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).
 
  • #4
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().
 
  • #5
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.
 
  • #6
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.
 
  • #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]?
 

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

1. What is a reverse dictionary key?

A reverse dictionary key is a key-value pair in a dictionary where the key is a value and the value is a list of keys that share that value. In other words, the key is used to retrieve the corresponding value, but in this case, the value can also be used to retrieve the key(s).

2. How is a reverse dictionary key different from a regular dictionary key?

A regular dictionary key is unique and can only have one value associated with it. In a reverse dictionary key, the value can be associated with multiple keys.

3. How can I use a reverse dictionary key in my research or work?

A reverse dictionary key can be useful for data analysis and organization. It allows for quick and efficient retrieval of keys based on a shared value, making it easier to identify patterns and relationships in data.

4. Can a reverse dictionary key have multiple values?

No, a reverse dictionary key can only have one value, but that value can be associated with multiple keys.

5. How can I create a reverse dictionary key in my own code?

To create a reverse dictionary key, you can use a for loop to iterate through the keys and values in a regular dictionary and add the keys to a list of values in the reverse dictionary. Alternatively, you can use the defaultdict class from the collections module in Python to automatically assign a list as the value for each key in the reverse dictionary.

Similar threads

Replies
5
Views
2K
Replies
7
Views
1K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
0
Views
2K
2
Replies
67
Views
11K
Replies
2
Views
11K
Replies
3
Views
3K
Replies
2
Views
2K
Back
Top