- 6

Please someone help me with this python code. Am finding it difficult to understand. Thanks

๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡ Create a Python dictionary that returns a list of values for each key. The key can be whatever type you want. Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it. Invent the dictionary yourself. Do not copy the design or items from some other source. Next consider the invert_dict function from Section 11.5 of your textbook. # From Section 11.5 of: # Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press. def invert_dict(d): inverse = dict() for key in d: val = d[key] if val not in inverse: inverse[val] = [key] else: inverse[val].append(key) return inverse Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.

27th May 2021, 9:42 PM
Ogorri ThankGod
Ogorri ThankGod - avatar
7 Answers
+ 1
Eashan Morajkar mmhh... rather than 'join' I would say 'store together': same initial values (wich becomes keys) could occur only once, so must be able to store multiple values (wich was initial keys)... however, don't post answer: OP must do it by himself ;P
28th May 2021, 4:09 PM
visph
visph - avatar
+ 3
good exercise ^^ where is your attempt? where are you stuck?
27th May 2021, 9:45 PM
visph
visph - avatar
0
I did not understand it correctly if input is {'1':1,'2':2,'3':3} Then should output be {'3':3,'2':2,'1':1}
28th May 2021, 7:41 AM
Eashan Morajkar
Eashan Morajkar - avatar
0
Eashan Morajkar if input is: { 'a': 1, 'b': 2, 'c': 1 } then output: { '1': ['a', 'c'], '2': ['b'] }
28th May 2021, 4:01 PM
visph
visph - avatar
0
visph so the code has to invert the keys with values and as there cant be similar keys the join has to join the values
28th May 2021, 4:04 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
visph He got the answer he had messaged me
28th May 2021, 4:16 PM
Eashan Morajkar
Eashan Morajkar - avatar
- 1
Thanks everyone for your help ๐Ÿ˜Š๐Ÿ˜Š
29th May 2021, 6:09 AM
Ogorri ThankGod
Ogorri ThankGod - avatar