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

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

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

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

  • PYTHON PROGRAM by using functions & dictionary or set. - must create a variable to hold...

    PYTHON PROGRAM by using functions & dictionary or set. - must create a variable to hold a dictionary or sets - must define a function that accepts dictionaries or sets as an argument A dictionary maps a set of objects (keys) to another set of objects (values). A Python dictionary is a mapping of unique keys to values. For e.g.: a dictionary defined as: released = { "iphone" : 2007, "iphone 3G": 2008, "iphone 3GS": 2009, "iphone 4" : 2010,...

  • In JavaScript define a function named 'lowInventory' which will be called with a dictionary mapping strings...

    In JavaScript define a function named 'lowInventory' which will be called with a dictionary mapping strings to numbers and which returns the number of key-value pairs whose value is lee than or equal to 10. Example: lowInventory({'milk':14,'butter':5,'bread':37,'jam':8}) must return 2 lowInventory{{'oil':16,'filter':29,'grease':42,'hydraulic fluid':18}) must return 0

  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ 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...

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