Python – Count frequency of Sublist in given list
Last Updated :
10 Mar, 2023
Given a List and a sublist, count occurrence of sublist in list.
Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation : No occurrence found.
Method #1 : Using list comprehension + slicing
In this, we test for each sublist of list extracted using slicing, if found, the element is added to list, at last length of list is computed using len().
Python3
test_list = [ 4 , 5 , 3 , 5 , 7 , 8 , 3 , 5 , 7 , 2 , 7 , 3 , 2 ]
print ( "The original list is : " + str (test_list))
sublist = [ 3 , 5 , 7 ]
res = len ([sublist for idx in range ( len (test_list)) if test_list[idx : idx + len (sublist)] = = sublist])
print ( "The sublist count : " + str (res))
|
Output
The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1) additional space is not required
Method #2 : Using zip_longest() + islice() + all() + loop
In this, we perform task of slicing using islice() and check if each element is matching using all(), zip_longest() helps in mapping elements to check for equality from sublist. Loop is used to get to index match with first element of sublist in list, to make it more efficient.
Python3
from itertools import zip_longest, islice
test_list = [ 4 , 5 , 3 , 5 , 7 , 8 , 3 , 5 , 7 , 2 , 7 , 3 , 2 ]
print ( "The original list is : " + str (test_list))
sublist = [ 3 , 5 , 7 ]
res = []
idx = 0
while True :
try :
idx = test_list.index(sublist[ 0 ], idx)
except ValueError:
break
if all (x = = y for (x, y) in zip_longest(sublist, islice(test_list, idx, idx + len (sublist)))):
res.append(sublist)
idx + = len (sublist)
idx + = 1
res = len (res)
print ( "The sublist count : " + str (res))
|
Output
The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2
Share your thoughts in the comments
Please Login to comment...