Python – Substitute digits using Dictionary
Last Updated :
16 May, 2023
Sometimes, while working with Python Dictionary, we can have a problem in which we need to perform substitution of digits of each element in List. This can lead to change in numbers. This kind of problem can occur in data preprocessing domains. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [45, 32], dig_map = {4 : 2, 3 : 8, 2 : 6, 5 : 7}
Output : [27, 86]
Input : test_list = [44, 44], dig_map = {4 : 2}
Output : [22, 22]
Method #1: Using loop This is brute way to solve this problem. In this, we iterate through each element in list and recreate it using mapped value after converting number to string.
Python3
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
print ("The original list is : " + str (test_list))
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
temp = []
for idx, ele in enumerate (test_list):
sub1 = str (ele)
if len (sub1) > 1 :
sub2 = ""
for j in sub1:
if int (j) in dig_map:
sub2 + = str (dig_map[ int (j)])
test_list[idx] = int (sub2)
else :
if ele in dig_map:
test_list[idx] = dig_map[ele]
print (" List after Digit Substitution : " + str (test_list))
|
Output :
The original list is : [45, 32, 87, 34, 21, 91]
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity: O(nm), where n is the length of the input list and m is the maximum number of digits in any element of the list.
Auxiliary Space: O(1), because the size of the additional space used by the algorithm is constant and does not depend on the size of the input.
Method #2: Using join() + list comprehension + str() + int() The combination of above functions can be used to solve this problem. In this, we perform the task of interconversion using str() and int(), join and list comprehension are used to bind the logic in form of shorthand.
Python3
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
print ("The original list is : " + str (test_list))
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
res = [ int (''.join([ str (dig_map[ int (ele)]) for ele in str (sub)])) for sub in test_list]
print (" List after Digit Substitution : " + str (res))
|
Output :
The original list is : [45, 32, 87, 34, 21, 91]
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity: O(n * m), where n is the length of the input list and m is the maximum number of digits in any element of the input list.
Auxiliary space: O(m), where m is the maximum number of digits in any element of the input list.
Method 3: map() function along with lambda function
Use the map() function to apply the lambda function to each element of the list. The lambda function takes an element x of the list and converts it to a string, then replaces each digit with its corresponding value from the digit mapping dictionary using a generator expression. Finally, we join the resulting string and convert it back to an integer.
Python3
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
print ( "The original list is : " , test_list)
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
res = list ( map ( lambda x: int (''.join( str (dig_map[ int (i)]) for i in str (x))), test_list))
print ( "List after Digit Substitution : " , res)
|
Output
The original list is : [45, 32, 87, 34, 21, 91]
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity: O(n * k), where n is the length of the input list and k is the maximum number of digits in any element of the input list.
Auxiliary space: O(n * k), where n is the length of the input list and k is the maximum number of digits in any element of the input list.
Method 4: using list comprehension and nested for loops
This program replaces each digit in a given list with a corresponding digit from a mapping dictionary. It uses a list comprehension and nested for loops to achieve this, and then prints the resulting list with the substituted digits.
Step-by-step approach:
- Create a list named test_list containing integers 45, 32, 87, 34, 21, and 91.
- Create a dictionary named dig_map with keys representing digits (1 to 9) and values representing the digit that replaces them.
- Using list comprehension and nested for loops, convert each integer in test_list to a string, replace each digit using the dig_map dictionary, join the digits together to form a new string, and then convert the new string back to an integer. Append each resulting integer to a new list named res.
- Print the list res which contains the modified integers obtained by replacing each digit in the original list using the dig_map dictionary.
Python3
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
res = [ int ("".join( str (dig_map[ int (j)]) for j in str (i))) for i in test_list]
print ( "List after Digit Substitution : " , res)
|
Output
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity:O(n * k), where n is the length of the input list and k is the maximum number of digits in a single element of the list.
Auxiliary space: O(n), because we create a new list res to store the substituted digits, which has the same length as the input list.
Method 5: Using bitwise operations
Step-by-step algorithm:
- Initialize the input list test_list and the digit mapping dictionary dig_map.
- Create a bitmask using bitwise operations to represent which digits in dig_map will be used for substitution.
- Use a list comprehension to iterate over each number n in test_list, and for each number:
- Convert the number n to a string.
- Iterate over each digit d in the string representation of n, and:
- Use the bitmask to check whether the digit `d` should be substituted using `dig_map`.
- If the digit `d` should be substituted, use `dig_map` to find the corresponding digit and add it to a list of substituted digits.
- If the digit `d` should not be substituted, add it to the list of substituted digits unchanged.
- Convert the list of substituted digits back to an integer and append it to a new list substituted_list.
- Assign the new list substituted_list to the variable test_list.
- Print the modified list test_list after digit substitution.
Python3
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
bitmask = sum ( 1 << d for d in dig_map.keys())
test_list = [ int (''.join( str (dig_map[ int (d)]) if ( 1 << int (d)) & bitmask else d for d in str (n))) for n in test_list]
print ( "List after Digit Substitution : " + str (test_list))
|
Output
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity: O(n*m), where n is the length of test_list and m is the maximum number of digits in any number in test_list. This is because the algorithm iterates over each number in test_list and then iterates over each digit in each number.
Auxiliary space complexity: O(n*m), where n is the length of test_list and m is the maximum number of digits in any number in test_list. This is because the algorithm creates a new list of substituted digits for each number in test_list, and each list can be as long as the number of digits in the original number.
Method #6: Using numpy and vectorized operations
- Import numpy module
- Convert test_list into a numpy array using np.array()
- Define a vectorized function using np.vectorize() that applies the digit mapping to each element of the array
- Apply the vectorized function to the array using the defined function
- Convert the resulting array into a list using .tolist()
Python3
import numpy as np
test_list = [ 45 , 32 , 87 , 34 , 21 , 91 ]
print ( "The original list is : " , test_list)
dig_map = { 1 : 4 , 4 : 2 , 3 : 8 , 2 : 6 , 7 : 5 , 9 : 3 , 8 : 9 , 5 : 7 }
vfunc = np.vectorize( lambda x: int (''.join( str (dig_map[ int (i)]) for i in str (x))))
arr = np.array(test_list)
res = vfunc(arr)
res = res.tolist()
print ( "List after Digit Substitution : " , res)
|
Output:
The original list is : [45, 32, 87, 34, 21, 91]
List after Digit Substitution : [27, 86, 95, 82, 64, 34]
Time complexity: O(n), where n is the number of elements in the test_list
Auxiliary space: O(n), due to the creation of a numpy array
Share your thoughts in the comments
Please Login to comment...