Question

In: Computer Science

In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...

In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).

Solutions

Expert Solution

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:

Below is the code to copy:
#CODE STARTS HERE----------------
from operator import itemgetter
def dictsort(d): #Function that sorts a dictionary
   d_list = [] #List to store tuples
   for k,v in d.items(): #Convert dict to a list of tuples
      d_list.append((k,v))#Tuple is of the form (key, value)

   #First sort the "key" in alphabetic order
   d_list.sort(key=itemgetter(0))
   #Then sort "value" in descending order
   d_list.sort(key = itemgetter(1), reverse=True)
   return d_list #Print result

#Test case
original_dict = {"d":5,"a":5,"f":5,"c":1,"p":8,"k":4}
print("Original dict: ",original_dict)
print("After sorting: ",dictsort(original_dict)) #Function call
#CODE ENDS HERE------------------

Related Solutions

Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
2. working with databases and files in python a) Write a function with prototype “def profound():”...
2. working with databases and files in python a) Write a function with prototype “def profound():” that will prompt the user to type something profound. It will then record the date and time using the “datetime” module and then append the date, time and profound line to a file called “profound.txt”. Do only one line per function call. Use a single write and f-string such that the file contents look like: 2020-10-27 11:20:22 -- Has eighteen letters does 2020-10-27 11:20:36...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
In python Write a program to implement RSA algorithm based on the public key. def encryptmessage():...
In python Write a program to implement RSA algorithm based on the public key. def encryptmessage(): def decryptmessage(): encryptmessage () decryptmessage () No in-built functions and third-party APIs will be allowed.
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes...
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes three integer parameters. It returns true if all three of the parameters are odd, and false otherwise. 2). (1 point) Write a sample call for this function. (Assume that you are calling from main) 3). (3 points) Write the definition (header and body) of the function.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Python: I am currently a function that will return a list of lists, with the frequency...
Python: I am currently a function that will return a list of lists, with the frequency and the zip code. However, I am having a difficult time organizing the list in decreasing order of frequency. We are asked to use sort () and sorted () instead of lambda. The function will find 5 digit zip codes, and when it sees a 9-digit zip code, the function needs to truncate the last 4 digits. The following is an example of the...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like this. 1) create a function popular. The dictionary called database contains each people's hobby and their excitement level. This function searches the dictionary and returns a list of that most popular hobby to least popular hobby. If multiple hobbies have the same number of popularity, follow alphabetical order. #What is popular means? The dictionary contains each people's hobby. The programmer should search the dictionary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT