Help coding Python 3 functions: The image below has the functions that need to b
ID: 3810270 • Letter: H
Question
Help coding Python 3 functions: The image below has the functions that need to be completed. Thank you!
The image below has the functions that need to be completed. def get_by_diett (animal_d, diet): Consider a dictionary, animal_d, where the keys are names of animals, and the values are descriptions of their diets. The function should search in the dictionary for animals that are labeled as diet, put them into a list and return the sorted list. Animal_d: a dictionary that maps animal names to their diet description. diet: the diet to search for. Return value: asciibetically sorted list of animals that are labeled as diet. Examples: get_by_diet ({'deer':' 'herbivore' 'bear': 'omnivore'}, 'herbivore') rightarrow ['deer'] get_by_diet ({'lion': 'carnivore', 'penguin': 'piscivore'}, 'omnivore') rightarrow [] def merge(d1, d2) Consider two dictionaries d1 and d2 of the same structure: with strings as keys and a list as values. This function creates a combined dictionary of the same structure but with the merged information from both d1 and d2: For a key s that belongs to only one of the original dictionaries in a pair likes s: xs, include the same pair in the new dictionary; For a key s that is present in both d1 and d2, like s: xs in d1 and s: ys in d2, includes: zs in the new dictionary where zs is the concatenation of xs and ys. Keep the duplicates in zs. Return value: the newly created dictionary. Do NOT update d1 or d2. Examples: d1 = {'cardinal': ['Virginia', 'Ohio'], 'meadowlark': ['Oregan']} d2 = {'robin': ['Connecticut']} d3 = {'meadowlark': ['Wyoming'] 'cardinal': ['Virginia', 'Illinois'], 'oriole': ['Maryland']} merge (d1, d2) rightarrow {' cardinal ':['Virginia', 'Ohio'], meadowlark': ['Oregan'], 'robin' ['Connecticut']} merge (d1, d3) rightarrow {' cardinal' ['Virginia' 'Ohio' 'Virginia' 'Illinois'], 'meadowlark' ['Oregan', 'Wyoming'], 'oriole': ['Maryland']}Explanation / Answer
Please find the answers below:
# I have defined the dictionary animal_d as follows. You can define anything as per your need.
animal_d = {'cheetah': 'carnivore','deer' : 'herbivore', 'bear' : 'omnivore', 'lion' : 'carnivore', 'dog': 'carnivore'}
#This is the function which returns us the sorted list of animals whose diet needs to be mentioned.
def get_by_diet(animal_d,diet):
animal_list = [] #Empty list.
if diet not in animal_d.values():
return []
else:
for animals in animal_d:
if animal_d[animals] == diet:
animal_list.append(animals)
return sorted(animal_list) #Return the sorted list of the animals.
animals = get_by_diet(animal_d, 'carnivore') #If we print animals, then it will give us our result.
print(animals)
______________________________________________________________________________
#These are the values that i chose for d1 , d2 and d3.
d1 = {'Cardinal':['Virginia','Ohio'], 'Meadowlark': ['Oregan']}
d2 = {'Robin' : ['Connecticut']}
d3 = {'Meadowlark':['Wyoming','Oregan'], 'Cardinal':['Virginia','Illinois'], 'Oriole': ['Maryland']}
#According to your need you can replace the values of d1,d2 and d3. You can take any values.
def merge(d1,d2):
for k in d2:
if k not in d1.keys():
d1[k] = d2[k]
else:
d1[k] = set(d1[k] + d2[k])
return d1
answer = merge(d1,d2)
print(answer) #Prints the merged dictionary.
_____________________________________________________________________________________
Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.