Ask Question
21 January, 17:18

1. Implement the function dict_intersect, which takes two dictionaries as parameters d1 and d2, and returns a new dictionary which contains only those keys which appear in both d1 and d2, whose values are a tuple of the corresponding values from d1 and d2.

E. g., dict_intersect ({'a': 'apple', 'b': 'banana'}, {'b': 'bee', 'c': 'cat'}) should return {'b': ('banana', 'bee') }

2. Implement the function consolidate, which accepts zero or more sequences in the star parameter seqs, and returns a dictionary whose keys consist of values found in those sequences, which in turn map to numbers indicating how many times each value appears across all the sequences.

E. g., consolidate ([1,2,3], [1,1,1], [2,4], [1]) should return the dictionary {1: 5, 2: 2, 3: 1, 4: 1}.

+5
Answers (1)
  1. 21 January, 18:10
    0
    1

    def dict_intersect (d1, d2) : #create dictionary

    d3={} #dictionaries

    for key1, value1 in d1. items () : #iterate through the loop

    if key1 in d2: #checking condition

    d3[key1] = (d1[key1], d2[key1]) #add the items into the dictionary

    return d 3

    print (dict_intersect ({'a': 'apple', 'b': 'banana'}, {'b': 'bee', 'c': 'cat'})) #display

    2

    def consolidate (*l1) : #create consolidate

    d3={} # create dictionary

    for k in l1: #iterate through the loop

    for number in k: #iterate through the loop d3[number]=d3. get (number, 0) + 1 #increment the value

    return d 3 #return

    print (consolidate ([1,2,3], [1,1,1], [2,4], [1])) #display

    Explanation:

    1

    Following are the description of program

    Create a dictionary i. e"dict_intersect (d1, d2) " in this dictionary created a dictionary d3. After that iterated the loop and check the condition. If the condition is true then add the items into the dictionary and return the dictionary d3. Finally print them that are specified in the given question.

    2

    Following are the description of program

    Create a dictionary consolidate inside that created a dictionary "d3". After that iterated the loop outer as well as inner loop and increment the value of items. Return the d3 dictionary and print the dictionary as specified in the given question.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “1. Implement the function dict_intersect, which takes two dictionaries as parameters d1 and d2, and returns a new dictionary which contains ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers