codingdirectional: Count the number of occurrences of each character and return it as a list of tuples in order of appearance

In this example we are going to create a function which will count the number of occurrences of each character and return it as a list of tuples in order of appearance. For example,

 ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)] 

The above is a 7 kyu question on CodeWars, this is the only question I can solve today after the first two fail attempts.

I am supposed to start the Blender project today but because I want to write a post for your people I have spent nearly an hour and a half working on those three python questions on CodeWars, I hope you people will really appreciate my effort and will share this post to help this website to grown.

 def ordered_count(input):      already = []     input_list = list(input)     return_list = []     for word in input_list:         if(word not in already):             return_list.append((word, input_list.count(word)))             already.append(word)      return return_list 

The solution above is short and solid, hope you like it.

Planet Python

Leave a Reply

Your email address will not be published. Required fields are marked *