Sorted Word frequency count using python

前端 未结 10 946
再見小時候
再見小時候 2020-11-28 06:00

I have to count the word frequency in a text using python. I thought of keeping words in a dictionary and having a count for each of these words.

Now if I have to so

10条回答
  •  天涯浪人
    2020-11-28 06:21

    There are few steps involved in this Problem :

    • Clean the Punctuations.
    • Sort the Array Based on Frequency.

      def wordCount(self,nums):
        nums = "Hello, number of transaction which happened, for,"
        nums=nums.lower().translate(None,string.punctuation).split()
        d = {}
        for i in nums:
          if i not in d:
          d[i] = 1
        else:
          d[i] = d[i]+1
       sorted_d = (sorted(d.items(), key = operator.itemgetter(1), reverse = True)
      
      for key,val in sorted_d:
       print key,val
      

提交回复
热议问题