Question

HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules,

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

Code:

#two dictionaries created
dic1={"john":12,"clint":23,"tony":17}
dic2={"steve":34,"rambo":29,"clint":10}

print("dictionaries before updating")
print("dictionary 1:")
print(dic1)
print("dictionary 2:")
print(dic2)
#function defined
def update_dict_with_rules(first,second):
   #if keys present in both elements then
   #summing them
   for i in first.keys():
       if i in second.keys():
           first[i]=first[i]+second[i]
   #if keys present in second dictionary ony
   #then inserting them into first
   for j in second.keys():
       if j not in first.keys():
           first[j]=second[j]

   #we made no changes to elements unique in first

#function called
update_dict_with_rules(dic1,dic2)
#printing dictionary 1

print("dictionaries after updating")
print("dictionary 1:")
print(dic1)
print("dictionary 2:")
print(dic2)

Code screenshot for indentation:

#two dictionaries created dic1={john:12,clint:23, tony:17} dic2={steve:34, rambo:29,clint:10} 1 2 3 4 5 6 7 8 9 1

Output:

C:\Users\GopichandM\Desktop\java>python dicty.py dictionaries before updating dictionary 1: {john: 12, clint: 23, tony:

code snippet:

#two dictionaries created
dic1={"john":12,"clint":23,"tony":17}
dic2={"steve":34,"rambo":29,"clint":10}

print("dictionaries before updating")
print("dictionary 1:")
print(dic1)
print("dictionary 2:")
print(dic2)
#function defined 
def update_dict_with_rules(first,second):
        #if keys present in both elements then
        #summing them
        for i in first.keys():
                if i in second.keys():
                        first[i]=first[i]+second[i]
        #if keys present in second dictionary ony
        #then inserting them into first
        for j in second.keys():
                if j not in first.keys():
                        first[j]=second[j]

        #we made no changes to elements unique in first

#function called
update_dict_with_rules(dic1,dic2)
#printing dictionary 1

print("dictionaries after updating")
print("dictionary 1:")
print(dic1)
print("dictionary 2:")
print(dic2)

**refer code screenshot for indentation**

Add a comment
Know the answer?
Add Answer to:
HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which...
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
  • HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which...

    HW13.13. Update a dictionary, following some rules, with another dictionary Define a function below, update_dict_with_rules, which takes two arguments: both are a dictionary of strings (keys) to integers (values). Complete the function such that it updates the first dictionary as follows: • For key-value pairs in both dictionaries, sum them • For key-value pairs in the second dictionary only, insert them into the first • Make no changes to key-value pairs unique to the first dictionary These updates should be...

  • 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"...

  • in Python, define a function named filterOut with three parameters. The first argument passed to the...

    in Python, define a function named filterOut with three parameters. The first argument passed to the function should be a list of dictionaries (the data), the second a string (a dictionary key), and the third another string (a dictionary value). This function must return a list of all the dictionaries from the input list which do NOT contain the indicated key:value pairing. in Python, define a function named filterInRange with four parameters. The first argument passed to the function should...

  • 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...

  • 1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys...

    1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are printed in alphabetical order. Note that...

  • 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...

  • 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...

  • Implement the function flip_dict which takes in a dictionary and returns a similar dictionary where the...

    Implement the function flip_dict which takes in a dictionary and returns a similar dictionary where the values have become the keys and the keys have become the values. In this problem, you can assume that each key value pair is unique, and no key or value will be repeated or used more than once. def flip_dict(dictionary): """Returns a flipped version of the original dictionary. >>> TAs = {"12pm-2pm": "brian", "10am-12pm": "sophia", "2pm-4pm": "alec"} >>> flipped_TAs = flip_dict(TAs) >>> sorted_keys =...

  • 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...

  • 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...

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