Wednesday, July 8, 2015

Iterate over a list of words and use a dictionary to keep track of the frequency(count) of each word.



s = ‘aaa bbb ccc ddd eee’
''.join(s.split())
Iterate over a list of words and use a dictionary to keep track of the frequency(count) of each word. for example
{‘one’:2, ‘two’:2, ‘three’:2}
Ans:
 def dic(words):
  a = {}
  for i in words:
    try:
      a[i] += 1
    except KeyError:
      a[i] = 1   
  return a

No comments:

Post a Comment