Skip to content

Instantly share code, notes, and snippets.

@roman-on
Created May 17, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman-on/e4f4f3fa06ada8b00783bc0396de29f2 to your computer and use it in GitHub Desktop.
Save roman-on/e4f4f3fa06ada8b00783bc0396de29f2 to your computer and use it in GitHub Desktop.
"""
We are nearing the end of the courses and at this stage we are in contact.
The function accepts as a dictionary parameter.
The function returns a new dictionary with "Reverse" mapping: Each key in the transferred dictionary
is a value in the returned dictionary and each value in the transferred dictionary is a key in the returned dictionary.
Instructions:
- The flip between keys and values ​​can create keys that appear more than once. Therefore,
display the values ​​in the dictionary returned as a list, which may contain one or more entries.
- Returned lists should be sorted (the values ​​in the dictionary may be of the same type).
- In the last chapter you got to know a number of actions when working with dictionary type objects.
In this exercise, read on the web for useful methods of working with a dictionary and explore additional actions for yourself.
Example of inverse_dict run function:
course_dict = {'I': 3, 'love': 3, 'self.py!': 2}
inverse_dict(course_dict)
{3: ['I', 'love'], 2: ['self.py!']}
"""
"""
INPUT:
course_dict = {'I': 3, 'love': 3, 'self.py!': 2}
RUN:
inverse_dict(course_dict)
OUTPUT:
{3: ['I', 'love'], 2: ['self.py!']}
"""
"""
The function returns a new dictionary with "Reverse" mapping: Each key in the transferred dictionary
is a value in the returned dictionary and each value in the transferred dictionary is a key in the returned dictionary.
"""
def inverse_dict(my_dict):
a = [] # New list
b = list(course_dict) # Laking a new variable and saving the dictionary list there
c = [] # New list
d = [] # New list
result = {} # New dict
z = len(b)-1 # Length of the
i = 0 # Zero the loop
for value in course_dict.values(): # Getting the values from the dict
a.append(value) # Adding the values to the variable "a"
while i < z: # Running the loop
# Condition: If the amount of the number from the list "a" is more than 1, and numbers equal to the number of the dict key, ......
# .....and the word not in the dictionary already
if a.count(a[i]) > 1 and a[i] == course_dict[b[i]] and not(b[i] in c):
c.append(b[i]) # Adding the word to variable "c"
result[a[i]] = c # Adding the word from variable "c" to my new made dict "result"
i += 1 # VERY IMPORTANT look where the +1 is located in the loop or the last condition not gonna work.
# Condition: If the amount of the number from the list "a" is equal to 1, and numbers equal to the number of the dict key, ......
# .....and the word not in the dictionary already
if a.count(a[i]) == 1 and a[i] == course_dict[b[i]] and not(b[i] in d):
d.append(b[i]) # Adding the word to variable "d"
result[a[i]] = d # Adding the word from variable "d" to my new made dict "result"
return result
def main():
<call the function>
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment