Python – Reform K digit elements
Given the Python list, reform the element to have K digits in a single element.
Input : test_list = [223, 67, 332, 1, 239, 2, 931], K = 2
Output : [22, 36, 73, 32, 12, 39, 29, 31]
Explanation : Elements reformed to assign 2 digits to each element.Input : test_list = [223, 67, 3327], K = 3
Output : [223, 673, 327]
Explanation : Elements reformed to assign 3 digits to each element.
Method #1 : Using slicing + join() + loop
In this, we perform the task of joining all elements to a single string, then slice K digits, and reconvert to a list.
Python3
# Python3 code to demonstrate working of # Reform K digit elements # Using slicing + join() + loop # initializing list test_list = [ 223 , 67 , 332 , 1 , 239 , 2 , 931 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 # converting to string temp = ''.join([ str (ele) for ele in test_list]) # getting K digit slices res = [] for idx in range ( 0 , len (temp), K): res.append( int (temp[idx: idx + K])) # printing result print ( "Reforming K digits : " + str (res)) |
The original list is : [223, 67, 332, 1, 239, 2, 931] Reforming K digits : [22, 36, 73, 32, 12, 39, 29, 31]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + join()
In this, the task of reforming the list is done using list comprehension.
Python3
# Python3 code to demonstrate working of # Reform K digit elements # Using list comprehension + join() # initializing list test_list = [ 223 , 67 , 332 , 1 , 239 , 2 , 931 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 # converting to string temp = ''.join([ str (ele) for ele in test_list]) # getting K digit slices # using 1 liner list comprehension res = [ int (temp[idx: idx + K]) for idx in range ( 0 , len (temp), K)] # printing result print ( "Reforming K digits : " + str (res)) |
The original list is : [223, 67, 332, 1, 239, 2, 931] Reforming K digits : [22, 36, 73, 32, 12, 39, 29, 31]
Time Complexity: O(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 list “test_list”.
Method #4: Using itertools and string concatenation
- Import the ‘itertools’ module.
- Convert each element of the original list to a string and concatenate it to a variable ‘temp’.
- Using the ‘grouper’ function from itertools, group ‘temp’ into groups of K.
- For each group, join the characters using the ‘join’ function and append the integer value of the resulting string to ‘res’.
- Print the final ‘res’ list.
Python3
# Python3 code to demonstrate working of # Reform K digit elements # Using itertools and string concatenation import itertools # initializing list test_list = [ 223 , 67 , 332 , 1 , 239 , 2 , 931 ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 2 # Converting to string temp = "".join( str (ele) for ele in test_list) # Getting K digit slices res = [ int (" ".join(group)) for group in itertools.zip_longest(*[iter(temp)]*K, fillvalue=" ")] # Printing the result print ( "Reforming K digits : " + str (res)) |
The original list is : [223, 67, 332, 1, 239, 2, 931] Reforming K digits : [22, 36, 73, 32, 12, 39, 29, 31]
Time complexity: O(N), where n is the length of the original list.
Auxiliary space: O(N), where n is the length of the original list (for storing the ‘temp’ string).
Please Login to comment...