Python – Step Frequency of elements in List
Last Updated :
15 Mar, 2023
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + defaultdict() The combination of above functions can be used to perform this task. In this, we just initialize list with a default value and increment its frequency using a loop.
Python3
from collections import defaultdict
test_list = [ 'gfg' , 'is' , 'best' , 'gfg' , 'is' , 'life' ]
print ( "The original list is : " + str (test_list))
res_d = defaultdict( int )
res = []
for ele in test_list:
res_d[ele] + = 1
res.append(res_d[ele])
print ( "Step frequency of elements is : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time complexity: O(n), where n is the number of elements in the input list test_list.
Auxiliary space: O(m), where m is the number of unique elements in the input list test_list. The defaultdict data structure is used in the code, which may occupy a space equivalent to the number of unique elements in the input list.
Method #2 : Using list comprehension + enumerate() The combination of above functions can be used to solve this problem. In this we just iterate and store the counter using enumerate().
Python3
from collections import defaultdict
test_list = [ 'gfg' , 'is' , 'best' , 'gfg' , 'is' , 'life' ]
print ( "The original list is : " + str (test_list))
res = [test_list[ : idx + 1 ].count(ele) for (idx, ele) in enumerate (test_list)]
print ( "Step frequency of elements is : " + str (res))
|
Output :
The original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Method #3 : Using for loop + count() method
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'gfg' , 'is' , 'life' ]
print ( "The original list is : " + str (test_list))
res = []
for ele in range ( 0 , len (test_list)):
res.append(test_list[:ele + 1 ].count(test_list[ele]))
print ( "Step frequency of elements is : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #4 : Using operator.countOf() method
Python3
import operator as op
test_list = [ 'gfg' , 'is' , 'best' , 'gfg' , 'is' , 'life' ]
print ( "The original list is : " + str (test_list))
res = []
for ele in range ( 0 , len (test_list)):
res.append(op.countOf(test_list[:ele + 1 ],test_list[ele]))
print ( "Step frequency of elements is : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...