How to count the frequency of the elements in an unordered list?

PythonCounterFrequencyCounting

Python Problem Overview


I need to find the frequency of elements in an unordered list

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

output->

b = [4,4,2,1,2]

Also I want to remove the duplicates from a

a = [1,2,3,4,5]

Python Solutions


Solution 1 - Python

In Python 2.7 (or newer), you can use collections.Counter:

import collections

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)

print(counter)
# Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})

print(counter.values())
# [4, 4, 2, 1, 2]

print(counter.keys())
# [1, 2, 3, 4, 5]

print(counter.most_common(3))
# [(1, 4), (2, 4), (3, 2)]

print(dict(counter))
# {1: 4, 2: 4, 3: 2, 5: 2, 4: 1}

If you are using Python 2.6 or older, you can download it here.

Solution 2 - Python

Note: You should sort the list before using groupby.

You can use groupby from itertools package if the list is an ordered list.

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
from itertools import groupby
[len(list(group)) for key, group in groupby(a)]

Output:

[4, 4, 2, 1, 2]

update: Note that sorting takes O(n log(n)) time.

Solution 3 - Python

Python 2.7+ introduces Dictionary Comprehension. Building the dictionary from the list will get you the count as well as get rid of duplicates.

>>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>> d = {x:a.count(x) for x in a}
>>> d
{1: 4, 2: 4, 3: 2, 4: 1, 5: 2}
>>> a, b = d.keys(), d.values()
>>> a
[1, 2, 3, 4, 5]
>>> b
[4, 4, 2, 1, 2]

Solution 4 - Python

To count the number of appearances:

from collections import defaultdict

appearances = defaultdict(int)

for curr in a:
    appearances[curr] += 1

To remove duplicates:

a = set(a) 

Solution 5 - Python

In Python 2.7+, you could use collections.Counter to count items

>>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>>
>>> from collections import Counter
>>> c=Counter(a)
>>>
>>> c.values()
[4, 4, 2, 1, 2]
>>>
>>> c.keys()
[1, 2, 3, 4, 5]

Solution 6 - Python

Counting the frequency of elements is probably best done with a dictionary:

b = {}
for item in a:
    b[item] = b.get(item, 0) + 1

To remove the duplicates, use a set:

a = list(set(a))

Solution 7 - Python

Here's another succint alternative using itertools.groupby which also works for unordered input:

from itertools import groupby

items = [5, 1, 1, 2, 2, 1, 1, 2, 2, 3, 4, 3, 5]

results = {value: len(list(freq)) for value, freq in groupby(sorted(items))}

results

{1: 4, 2: 4, 3: 2, 4: 1, 5: 2}

Solution 8 - Python

You can do this:

import numpy as np
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
np.unique(a, return_counts=True)

Output:

(array([1, 2, 3, 4, 5]), array([4, 4, 2, 1, 2], dtype=int64))

The first array is values, and the second array is the number of elements with these values.

So If you want to get just array with the numbers you should use this:

np.unique(a, return_counts=True)[1]

Solution 9 - Python

from collections import Counter
a=["E","D","C","G","B","A","B","F","D","D","C","A","G","A","C","B","F","C","B"]

counter=Counter(a)

kk=[list(counter.keys()),list(counter.values())]

pd.DataFrame(np.array(kk).T, columns=['Letter','Count'])

Solution 10 - Python

I would simply use scipy.stats.itemfreq in the following manner:

from scipy.stats import itemfreq

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

freq = itemfreq(a)

a = freq[:,0]
b = freq[:,1]

you may check the documentation here: http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.stats.itemfreq.html

Solution 11 - Python

seta = set(a)
b = [a.count(el) for el in seta]
a = list(seta) #Only if you really want it.

Solution 12 - Python

Data. Let's say we have a list:

fruits = ['banana', 'banana', 'apple', 'banana']

Solution. Then we can find out how many of each fruit we have in the list by doing this:

import numpy as np    
(unique, counts) = np.unique(fruits, return_counts=True)
{x:y for x,y in zip(unique, counts)}

Output:

{'banana': 3, 'apple': 1}

Solution 13 - Python

This answer is more explicit

a = [1,1,1,1,2,2,2,2,3,3,3,4,4]

d = {}
for item in a:
    if item in d:
        d[item] = d.get(item)+1
    else:
        d[item] = 1

for k,v in d.items():
    print(str(k)+':'+str(v))

# output
#1:4
#2:4
#3:3
#4:2

#remove dups
d = set(a)
print(d)
#{1, 2, 3, 4}

Solution 14 - Python

For your first question, iterate the list and use a dictionary to keep track of an elements existsence.

For your second question, just use the set operator.

Solution 15 - Python

I am quite late, but this will also work, and will help others:

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
freq_list = []
a_l = list(set(a))

for x in a_l:
	freq_list.append(a.count(x))


print 'Freq',freq_list
print 'number',a_l

will produce this..

Freq  [4, 4, 2, 1, 2]
number[1, 2, 3, 4, 5]

Solution 16 - Python

def frequencyDistribution(data):
    return {i: data.count(i) for i in data}   

print frequencyDistribution([1,2,3,4])

...

 {1: 1, 2: 1, 3: 1, 4: 1}   # originalNumber: count

Solution 17 - Python

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

# 1. Get counts and store in another list
output = []
for i in set(a):
    output.append(a.count(i))
print(output)

# 2. Remove duplicates using set constructor
a = list(set(a))
print(a)
  1. Set collection does not allow duplicates, passing a list to the set() constructor will give an iterable of totally unique objects. count() function returns an integer count when an object that is in a list is passed. With that the unique objects are counted and each count value is stored by appending to an empty list output
  2. list() constructor is used to convert the set(a) into list and referred by the same variable a

Output

D:\MLrec\venv\Scripts\python.exe D:/MLrec/listgroup.py
[4, 4, 2, 1, 2]
[1, 2, 3, 4, 5]

Solution 18 - Python

Simple solution using a dictionary.

def frequency(l):
     d = {}
     for i in l:
        if i in d.keys():
           d[i] += 1
        else:
           d[i] = 1

     for k, v in d.iteritems():
        if v ==max (d.values()):
           return k,d.keys()

print(frequency([10,10,10,10,20,20,20,20,40,40,50,50,30]))

Solution 19 - Python

To find unique elements in the list:

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
a = list(set(a))

To find the count of unique elements in a sorted array using dictionary:

def CountFrequency(my_list): 
# Creating an empty dictionary  
freq = {} 
for item in my_list: 
    if (item in freq): 
        freq[item] += 1
    else: 
        freq[item] = 1

for key, value in freq.items(): 
    print ("% d : % d"%(key, value))

# Driver function 
if __name__ == "__main__":  
my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] 

CountFrequency(my_list)

Reference:

GeeksforGeeks

Solution 20 - Python

#!usr/bin/python
def frq(words):
    freq = {}
    for w in words:
            if w in freq:
                    freq[w] = freq.get(w)+1
            else:
                    freq[w] =1
    return freq

fp = open("poem","r")
list = fp.read()
fp.close()
input = list.split()
print input
d = frq(input)
print "frequency of input\n: "
print d
fp1 = open("output.txt","w+")
for k,v in d.items():
fp1.write(str(k)+':'+str(v)+"\n")
fp1.close()

Solution 21 - Python

num=[3,2,3,5,5,3,7,6,4,6,7,2]
print ('\nelements are:\t',num)
count_dict={}
for elements in num:
	count_dict[elements]=num.count(elements)
print ('\nfrequency:\t',count_dict)

Solution 22 - Python

from collections import OrderedDict
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
def get_count(lists):
    dictionary = OrderedDict()
    for val in lists:
        dictionary.setdefault(val,[]).append(1)
    return [sum(val) for val in dictionary.values()]
print(get_count(a))
>>>[4, 4, 2, 1, 2]

To remove duplicates and Maintain order:

list(dict.fromkeys(get_count(a)))
>>>[4, 2, 1]

Solution 23 - Python

i'm using Counter to generate a freq. dict from text file words in 1 line of code

def _fileIndex(fh):
''' create a dict using Counter of a
flat list of words (re.findall(re.compile(r"[a-zA-Z]+"), lines)) in (lines in file->for lines in fh)
'''
return Counter(
    [wrd.lower() for wrdList in
     [words for words in
      [re.findall(re.compile(r'[a-zA-Z]+'), lines) for lines in fh]]
     for wrd in wrdList])

Solution 24 - Python

Another approach of doing this, albeit by using a heavier but powerful library - NLTK.

import nltk

fdist = nltk.FreqDist(a)
fdist.values()
fdist.most_common()

Solution 25 - Python

Found another way of doing this, using sets.

#ar is the list of elements
#convert ar to set to get unique elements
sock_set = set(ar)

#create dictionary of frequency of socks
sock_dict = {}

for sock in sock_set:
    sock_dict[sock] = ar.count(sock)

Solution 26 - Python

For an unordered list you should use:

[a.count(el) for el in set(a)]

The output is

[4, 4, 2, 1, 2]

Solution 27 - Python

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
d = {}
[d.setdefault(el, []).append(1) for el in a]
counts = {k: len(v) for k, v in d.items()}
counts
# {1: 4, 2: 4, 3: 2, 4: 1, 5: 2}

Solution 28 - Python

Yet another solution with another algorithm without using collections:

def countFreq(A):
   n=len(A)
   count=[0]*n                     # Create a new list initialized with '0'
   for i in range(n):
      count[A[i]]+= 1              # increase occurrence for value A[i]
   return [x for x in count if x]  # return non-zero count

Solution 29 - Python

You can use the in-built function provided in python

l.count(l[i])


  d=[]
  for i in range(len(l)):
        if l[i] not in d:
             d.append(l[i])
             print(l.count(l[i])

The above code automatically removes duplicates in a list and also prints the frequency of each element in original list and the list without duplicates.

Two birds for one shot ! X D

Solution 30 - Python

This approach can be tried if you don't want to use any library and keep it simple and short!

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
marked = []
b = [(a.count(i), marked.append(i))[0] for i in a if i not in marked]
print(b)

o/p

[4, 4, 2, 1, 2]

Solution 31 - Python

For the record, a functional answer:

>>> L = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>> import functools
>>> >>> functools.reduce(lambda acc, e: [v+(i==e) for i, v in enumerate(acc,1)] if e<=len(acc) else acc+[0 for _ in range(e-len(acc)-1)]+[1], L, [])
[4, 4, 2, 1, 2]

It's cleaner if you count zeroes too:

>>> functools.reduce(lambda acc, e: [v+(i==e) for i, v in enumerate(acc)] if e<len(acc) else acc+[0 for _ in range(e-len(acc))]+[1], L, [])
[0, 4, 4, 2, 1, 2]

An explanation:

  • we start with an empty acc list;
  • if the next element e of L is lower than the size of acc, we just update this element: v+(i==e) means v+1 if the index i of acc is the current element e, otherwise the previous value v;
  • if the next element e of L is greater or equals to the size of acc, we have to expand acc to host the new 1.

The elements do not have to be sorted (itertools.groupby). You'll get weird results if you have negative numbers.

Solution 32 - Python

One more way is to use a dictionary and the list.count, below a naive way to do it.

dicio = dict()

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

b = list()

c = list()

for i in a:

   if i in dicio: continue 

   else:
    
      dicio[i] = a.count(i)
   
      b.append(a.count(i))
    
      c.append(i)

print (b)

print (c)

Solution 33 - Python

a=[1,2,3,4,5,1,2,3]
b=[0,0,0,0,0,0,0]
for i in range(0,len(a)):
	b[a[i]]+=1

Solution 34 - Python

str1='the cat sat on the hat hat'
list1=str1.split();
list2=str1.split();

count=0;
m=[];

for i in range(len(list1)):
    t=list1.pop(0);
    print t
    for j in range(len(list2)):
        if(t==list2[j]):
            count=count+1;
            print count
    m.append(count)
    print m
    count=0;
#print m

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBruceView Question on Stackoverflow
Solution 1 - PythonunutbuView Answer on Stackoverflow
Solution 2 - PythonNadia AlramliView Answer on Stackoverflow
Solution 3 - PythonAmjithView Answer on Stackoverflow
Solution 4 - PythonIdan KView Answer on Stackoverflow
Solution 5 - PythonYOUView Answer on Stackoverflow
Solution 6 - PythonlindelofView Answer on Stackoverflow
Solution 7 - PythonrbentoView Answer on Stackoverflow
Solution 8 - PythonEvgenii PavlovView Answer on Stackoverflow
Solution 9 - PythonAnirban LahiriView Answer on Stackoverflow
Solution 10 - Pythonuser2757762View Answer on Stackoverflow
Solution 11 - PythonlprsdView Answer on Stackoverflow
Solution 12 - PythonjobimaView Answer on Stackoverflow
Solution 13 - PythonCorey RicheyView Answer on Stackoverflow
Solution 14 - Pythont3rseView Answer on Stackoverflow
Solution 15 - PythonjaxView Answer on Stackoverflow
Solution 16 - Pythonuser2422819View Answer on Stackoverflow
Solution 17 - PythonSai KiranView Answer on Stackoverflow
Solution 18 - PythonoshaikenView Answer on Stackoverflow
Solution 19 - PythonGaurav BansalView Answer on Stackoverflow
Solution 20 - PythonamruthaView Answer on Stackoverflow
Solution 21 - Pythonchandan anandView Answer on Stackoverflow
Solution 22 - PythonPradamView Answer on Stackoverflow
Solution 23 - PythonrobertoView Answer on Stackoverflow
Solution 24 - PythonAbhishek PoojaryView Answer on Stackoverflow
Solution 25 - PythonAbhishek PoojaryView Answer on Stackoverflow
Solution 26 - PythonLuigi TiburziView Answer on Stackoverflow
Solution 27 - Pythond.bView Answer on Stackoverflow
Solution 28 - PythonReza AbtinView Answer on Stackoverflow
Solution 29 - PythonVarun ShaandheshView Answer on Stackoverflow
Solution 30 - PythonNamrata TolaniView Answer on Stackoverflow
Solution 31 - PythonjferardView Answer on Stackoverflow
Solution 32 - PythonValquiria F. PereiraView Answer on Stackoverflow
Solution 33 - PythonAMITH M SView Answer on Stackoverflow
Solution 34 - PythonOsama ZahidView Answer on Stackoverflow