Q. Write a Python program that accepts a value and checks whether the inputted value is part of given dictionary or not. If it is present, check for the frequency of its occurrence and print the corresponding key otherwise print an error message.
Answer :-
dic = eval(input("Enter a dictionary :- ")) val = eval(input("Enter a value :-")) lst = list( dic.values() ) if val in lst : print("Frequency of occurrence of ",val ," is ", lst.count( val )) print("Its key are :-" ) for i in dic : if dic [i] == val : print(i) else : print("Not found !!")
Output :-
Enter a dictionary :- {1:10, 2.5:20, 3:30, 4:40, 5:50, 5.5 : 50 , 6:60, 7:70}
Enter a value :-50
Frequency of occurrence of 50 is 2
Its key are :-
5
5.5
>>>
Why u hve used list there???
ReplyDeletebecause I am extracting keys from dictionary.
Deletenice code
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )