Q. Complete the following code and predict the output:
# Data to count.
names = ['tern', 'goose', 'goose', 'hawk', 'tern', 'goose', 'tern']
#Build a dictionary of frequencies.
freq = ___________ #creates an empty dictionary
for name in names:
# Already seen, so increment count by one.
if ____ in ____ : # check for existence of name in dictionary freq
freq[name] = freq[name] + 1:
# Never seen before, so add to dictionary.
else:
freq[name] = 1
# Display.
print (freq)
Answer :-
Correct Program :-
names = ['tern', 'goose', 'goose', 'hawk', 'tern', 'goose', 'tern'] freq = {} for name in names: if name in freq.keys() : freq[name] = freq[name] + 1 else: freq[name] = 1 print (freq)
Output :-
{'tern': 3, 'goose': 3, 'hawk': 1}
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )