Question

Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if t...


Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if there is an entry (b, c) in d2, then the entry (a, c) should be added to the new dictionary.
For example, if d1 is {2:3, 8:19, 6:4, 5:12} and d2 is {2:5, 4:3, 3:9}, then the new dictionary should be {2:9, 6:3}
Associate the new dictionary with the variable d3
0 0
Add a comment Improve this question Transcribed image text
Answer #1
3) ans:

Required Code Screenshots:

Create an empty dictionary and associate it with the #name d3 d3 For every (a, b) present #in the dictionary dl. for (a,b) in d1.items): Check if there is a value (b, c) #in the dictionary d2 or not. if b in d2: #If there is a value (b, c) #in the dictionary d2 then, add (a, c) to the #dictionary d3. d3 ad2 [b]

Required Code To Copy:

#Create an empty dictionary

#and associate it with the

#name d3.

d3 = {}

#For every (a, b) present

#in the dictionary d1.

for (a,b) in d1.items():

    #Check if there is a value (b, c)

    #in the dictionary d2 or not.

    if b in d2:

        #If there is a value (b, c)

        #in the dictionary d2 then,

        #add (a, c) to the

        #dictionary d3.

        d3[a] = d2[b]

Complete Code Screenshots (To check the execution of the above given code):

Sample Output:

Code To Copy:

d1 = {2:3, 8:19, 6:4, 5:12}

d2 = {2:5, 4:3, 3:9}

#Create an empty dictionary

#and associate it with the

#name d3.

d3 = {}

#For every (a, b) present

#in the dictionary d1.

for (a,b) in d1.items():

  #Check if there is a value (b, c)

    #in the dictionary d2 or not.

    if b in d2:

        #If there is a value (b, c)

        #in the dictionary d2 then,

        #add (a, c) to the

        #dictionary d3.

        d3[a] = d2[b]

#Display the

#dictionary d3.

print(d3)

Add a comment
Know the answer?
Add Answer to:
Given dictionaries, d1 and d2, create a new dictionary with the following property: for each entry (a, b) in d1, if t...
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
  • Dictionaries

    1)You are given a variable, wwII_battle_winners , that is associated with a dictionary that maps the names of World War II battles to the victors of WWII battles. Writesome code that associates a sorted list of the battle names that appear as keys in this dictionary with the variable battles .2)The inverse of a map is a new map where the values of the original map become the keys of the new map and the keys become the values. For...

  • Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries:...

    Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries: - Parameters: file pointer (fp) - Returns : 3 dictionaries of list of tuples - The function displays nothing b) You must use the csv.reader because some fields have commas in them. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing...

  • BlockPy: #43.1) Dictionaries vs. Tuples Create a dictionary to store the dimensions for a box. Then,...

    BlockPy: #43.1) Dictionaries vs. Tuples Create a dictionary to store the dimensions for a box. Then, create a tuple to store the dimensions. Finally, give an example of accessing the same value in both the dictionary and tuple representation and printing them. You should only print one value from each of the two data structures. Length: 6 Width: 8 Height: 4 Console: Feedback: "Ready* D History Run Blocks Split EText EReset Import datasets Upload dimension_dict = {'Length': 6, 'Width': 8,...

  • Consider the following method that is intended to determine if the double values d1 and d2...

    Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal. /** @return true if d1 and d2 are within the specified tolerance, * false otherwise */ public boolean almostEqual(double d1, double d2, double tolerance) { /* missing code */ } Which of the following should replace /* missing code */ so...

  • A payoff table is given as State of Nature Decision s1 s2 s3 d1 250 750...

    A payoff table is given as State of Nature Decision s1 s2 s3 d1 250 750 500 d2 300 - 250 1200 d3 500 500 600 a. What choice should be made by the optimistic decision maker? b. What choice should be made by the conservative decision maker? c. What decision should be made under minimax regret? d. If the probabilities of d1, d2, and d3 are .2, .5, and .3, respectively, then what choice should be made under expected...

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

  • Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...

    Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....

  • Python Question 5 2 pts We have a dictionary d2 (NOTE THE VARIABLE NAME CHANGE), defined...

    Python Question 5 2 pts We have a dictionary d2 (NOTE THE VARIABLE NAME CHANGE), defined as follows, which maps words to the pages in a book that contain those words: d2-computer': [e, 11), programming [1, 19, 41, 60, 65, 89], is: [2, 54, 66], the: [3, 34, 48, 62, 76, 86, 1e1], process': [4, 87], of: [S, 36, 51, 64, 71, 78, 88, 10e], designing [6], and' [7, 30, 33, 1e6], building [8], an [9], executable': [10], program': [12,...

  • PYHTON Try! Consider a dictionary for the month months Dictionary = { 1 : "January", 2...

    PYHTON Try! Consider a dictionary for the month months Dictionary = { 1 : "January", 2 : "February", 3 : "March", 4: "April", 5 : "May", 6 : "June", 7 : "July", 8: "August", 9 : "September", 10 : "October", 11: "November", 12 : "December" } . Prints out all the items, both key and value, in the dictionary Prints out all the keys in the dictionary Prints out just the values in the dictionary Loops though using the keys...

  • Floats.java Use the following calculations for di and d2 in your code: double di = 0;...

    Floats.java Use the following calculations for di and d2 in your code: double di = 0; // add .1 to 0 eleven times d1 += 1; // 1 d1 += .1; // 2 d1 += 1; // 3 d1 += 1; // 4 d1 += 1; // 5 d1 += .1; 1/6 d1 += 1; /17 d1 += .1; 118 d1 += 1; // 9 d1 += 1; // 10 d1 += 1; // 11 double d2 = 1 *...

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