Given a list, the task is to write a Python program to replace the grouping of the consecutive elements with a product of the frequency and Item.
Input : test_list = [3, 3, 3, 3, 6, 7, 5, 5, 5, 8, 8, 6, 6, 6, 6, 6, 1, 1, 1, 2, 2]
Output : [12, 6, 7, 15, 16, 30, 3, 4]
Explanation : 3 occurs 4 times in consecution hence, 3*4 = 12, the result.Input : test_list = [3, 3, 3, 3, 6, 7, 5, 5, 5]
Output : [12, 6, 7, 15]
Explanation : 5 occurs 3 times in consecution hence, 5*3 = 15, the result.
Method 1: Using loop.
In this, the element is compared with the previous element for the decision of group end, if elements are different, a product of count till now and element is appended as result.
Python3
# Python3 code to demonstrate working of # Equal Consecution Product # Using loop # initializing list test_list = [ 3 , 3 , 3 , 3 , 6 , 7 , 5 , 5 , 5 , 8 , 8 , 6 , 6 , 6 , 6 , 6 , 1 , 1 , 1 , 2 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) res = [] count = 1 for idx in range ( 1 , len (test_list)): # checking with prev element if test_list[idx - 1 ] ! = test_list[idx]: # appending product res.append((test_list[idx - 1 ] * count)) count = 0 count + = 1 res.append((test_list[ - 1 ] * count)) # printing result print ( "Elements after equal Consecution product : " + str (res)) |
The original list is : [3, 3, 3, 3, 6, 7, 5, 5, 5, 8, 8, 6, 6, 6, 6, 6, 1, 1, 1, 2, 2] Elements after equal Consecution product : [12, 6, 7, 15, 16, 30, 3, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using groupby() + sum()
In this, groups are formed using groupby() and summation of the grouped elements gives the required product.
Python3
# Python3 code to demonstrate working of # Equal Consecution Product # Using groupby() + sum() from itertools import groupby # initializing list test_list = [ 3 , 3 , 3 , 3 , 6 , 7 , 5 , 5 , 5 , 8 , 8 , 6 , 6 , 6 , 6 , 6 , 1 , 1 , 1 , 2 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) # creating Consecution groups and summing for required values res = [ sum (group) for k, group in groupby(test_list)] # printing result print ( "Elements after equal Consecution product : " + str (res)) |
The original list is : [3, 3, 3, 3, 6, 7, 5, 5, 5, 8, 8, 6, 6, 6, 6, 6, 1, 1, 1, 2, 2] Elements after equal Consecution product : [12, 6, 7, 15, 16, 30, 3, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 : Using list comprehensions:
Approach:
- Initialize the list:
- Use list comprehensions to get a list of tuples where the first element is the unique value and the second element is the number of consecutive occurrences of that value:
- Use another list comprehension to get a list of products of the unique values and their consecutive counts:
- Print the original list and the resulting list:
Python3
# Python3 code to demonstrate working of # Equal Consecution Product # Using list comprehensions # importing modules from itertools import groupby # initializing list test_list = [ 3 , 3 , 3 , 3 , 6 , 7 , 5 , 5 , 5 , 8 , 8 , 6 , 6 , 6 , 6 , 6 , 1 , 1 , 1 , 2 , 2 ] # using list comprehensions to get a list of tuples where the first element is the unique value and the second element is the number of consecutive occurrences of that value consecutive_counts = [(k, len ( list (g))) for k, g in groupby(test_list)] # using another list comprehension to get a list of products of the unique values and their consecutive counts result = [x[ 0 ] * x[ 1 ] for x in consecutive_counts] # printing the original list and the resulting list print ( "The original list is : " + str (test_list)) print ( "Elements after equal Consecution product : " + str (result)) |
The original list is : [3, 3, 3, 3, 6, 7, 5, 5, 5, 8, 8, 6, 6, 6, 6, 6, 1, 1, 1, 2, 2] Elements after equal Consecution product : [12, 6, 7, 15, 16, 30, 3, 4]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list, because we need to store the consecutive counts of the unique values in a separate list.
Method 4 : Using a dictionary to count the consecutive occurrences of each unique value in the list.
The steps would be:
- Initialize an empty dictionary to store the consecutive counts.
- Initialize a counter variable to keep track of the consecutive occurrences of each unique value.
- Iterate through the list, checking if the current value is equal to the previous value. If it is, increment the counter. If it is not, add the previous value and its count to the dictionary and reset the counter to 1.
- Add the last value and its count to the dictionary.
- Iterate through the dictionary, computing the product of each key-value pair.
- Return the resulting list of products.
Python3
# Python3 code to demonstrate working of # Equal Consecution Product # Using dictionary # initializing list test_list = [ 3 , 3 , 3 , 3 , 6 , 7 , 5 , 5 , 5 , 8 , 8 , 6 , 6 , 6 , 6 , 6 , 1 , 1 , 1 , 2 , 2 ] # using dictionary to get consecutive counts of each unique value consecutive_counts = {} counter = 0 for i in range ( len (test_list)): if i = = 0 or test_list[i] = = test_list[i - 1 ]: counter + = 1 else : consecutive_counts[test_list[i - 1 ]] = counter counter = 1 consecutive_counts[test_list[ - 1 ]] = counter # computing the products of the consecutive counts result = [k * v for k, v in consecutive_counts.items()] # printing the original list and the resulting list print ( "The original list is : " + str (test_list)) print ( "Elements after equal Consecution product : " + str (result)) |
The original list is : [3, 3, 3, 3, 6, 7, 5, 5, 5, 8, 8, 6, 6, 6, 6, 6, 1, 1, 1, 2, 2] Elements after equal Consecution product : [12, 30, 7, 15, 16, 3, 4]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m), where m is the number of unique values in the input list (for the dictionary).