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

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 appending items and count to a 2d list l2
for i in l1:
    row=[]
    ct=0
    ct=l1.count(i)
    row.append(i)
    row.append(ct)
    l2.append(row)
#above part of code will have duplicates in it
#using below for loop we are trying to eliminate dups of list l2
for j in l2:
    #if will check for items in list and will append only if
    #element doesn't exists
    if j not in unq_l2:
        unq_l2.append(j)
#will contains unique items
print(unq_l2)


Method2. using if-else and loops

l1=[3,5,7,2,3,5,9.1]
l2=[]
unq_l=[]
for i in range(0,len(l1)):
    a=0
    row=[]
    if i not in l2:
        for j in range(0,len(l1)):
            if l1[i]==l1[j]:
                a=a+1
        row.append(l1[i])
        row.append(a)
        l2.append(row)
#print(l2)       
for j in l2:
    if j not in unq_l:
        unq_l.append(j)
       
print(unq_l)


Python Dictionaries: A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and tuples.
Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon : whereas each key is separated by a ‘comma’.


Method3. writing output to python dictionary

l1=[3,5,7,2,3,5,9.1]
my_dict = {} #declaring a dictionary
for item in l1:
    #if an item exists in dictionary then increase count
    if item in my_dict:
        my_dict[item] += 1
    else:
        my_dict[item] = 1
print(my_dict)

In method 3, output is written to a dictionary, denoted by {}, where in dictionary, '3' is the key and '2' is the value, which is count of element.

I hope I am able to explain my point in this scenario. I will be coming up with more scenarios on python and ETL using python.

Thanks for reading my content. A feedback from my readers will help me to enrich my content as well. So do share your views.

Thanks
Vivek Chaudhary


Comments

  1. Perhaps you should also include:

    ```
    from collections import Counter

    l1=[3, 5, 7, 2, 3, 5, 9.1]
    Counter(l1)
    ```

    ReplyDelete

Post a Comment