Posts

Showing posts from April, 2019

count occurrence of element of a 1-d list and write to 2-d list

Image
Hello Readers, In this article I am covering a scenario that I had learnt recently and love to share with my fellow coders. Agenda of article: Given a one dimension list of numbers. Count the number of occurrence of each element and write the output to a 2-d list and dictionary, using various methods. In this article we will go through python objects such as lists and dictionaries. Lets develop a basic understanding of what we are trying to achieve. let's say we have a 1-d list l1 as l1=[3,5,7,2,3,5,9.1] and in output we want a 2-d list which contains numbers and count of occurrences and output should look like: [[3, 2], [5, 2], [7, 1], [2, 1], [9.1, 1]] where 3 is item and 2 is count of occurrences. Lets develop the code and build understanding of the same as we proceed. And to visualize the code better I have shared the output from my Jupyter Notebook as well. Method1. Using count method l1=[3,5,7,2,3,5,9.1] l2=[] unq_l2=[] #iterating over list items and appe...