0

Suppose that we have the following list:

["aa", "abc", "aa", "bc", "abc", "abc", "a"]

I would like to get the following result about the occurrence of each element: "abc": 3 "aa" 2 "a": 1 "bc": 1

What would the best way in this case? The following is an approach I came up with.

#!/usr/bin/python

word_list = ["aa", "abc", "aa", "bc", "abc", "abc", "a"]

word_count_list = [[word, word_list.count(word)] for word in set(word_list)]
print (sorted(word_count_list, key = lambda word_count : word_count[1], reverse=True))

But I'm not quite sure whether the above approach is efficient. Could you suggest a good way for doing this?

chanwcom
  • 4,420
  • 8
  • 37
  • 49

0 Answers0