Question

PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

PYTHON 3:

Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in which it chooses a random key in the dictionary and removes that key-value pair from the dictionary. The function does not return any values since its purpose is to alter the dictionary. Hint: To select a random key, make a list out of the keys in the dictionary and use methods from the random module to choose one of the list elements. The following shows several example runs of the function. Remember that your runs may differ from what is seen below since the elements eliminated are chosen at random:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random

def removeRand(d, n):

if n == len(d):

keys = []

for one in d:

keys.append(one)

for i in range(n):

del(d[keys[i]])

print("Yo")

elif n < len(d):

keys = []

for one in d:

keys.append(one)

for i in range(n):

one = random.randint(0, len(keys)-1)

del(d[keys[one]])

d = {1:"hey", 2:"there", 3:"it", 4:"works"}

removeRand(d, 4)

print(d)

# Output: {1: 'hey', 3: 'it'}

Add a comment
Know the answer?
Add Answer to:
PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

    Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course. For example, the following...

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of...

    PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of the parameter dictionary are words and the corresponding values are lists of synonyms (synonyms are words which have the same or nearly the same meaning). The function removes all the synonyms which contain the letter 'e' or 'E') from each corresponding list of synonyms. As well, the function sorts each corresponding list of synonyms. Note: the testing code makes use of the print_dict_in_key_order(a_dict) which...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

  • This script will create a dictionary whose keys are all the directories listed in thePATH system...

    This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT