Open In App

Python – Substitute digits using Dictionary

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 code to demonstrate working of
# Substitute digits using Dictionary
# Using loop
 
# initializing list
test_list = [45, 32, 87, 34, 21, 91]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing digit mapping
dig_map = {1 : 4, 4 : 2, 3 : 8, 2 : 6, 7 : 5, 9 : 3, 8 : 9, 5 : 7}
 
# Substitute digits using Dictionary
# Using loop
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]
 
# printing result
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 code to demonstrate working of
# Substitute digits using Dictionary
# Using join() + list comprehension + str() + int()
 
# initializing list
test_list = [45, 32, 87, 34, 21, 91]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing digit mapping
dig_map = {1 : 4, 4 : 2, 3 : 8, 2 : 6, 7 : 5, 9 : 3, 8 : 9, 5 : 7}
 
# Substitute digits using Dictionary
# Using join() + list comprehension + str() + int()
res = [int(''.join([str(dig_map[int(ele)]) for ele in str(sub)])) for sub in test_list]
 
# printing result
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.




# initializing list
test_list = [45, 32, 87, 34, 21, 91]
 
# printing original list
print("The original list is : ", test_list)
 
# initializing digit mapping
dig_map = {1: 4, 4: 2, 3: 8, 2: 6, 7: 5, 9: 3, 8: 9, 5: 7}
 
# Substitute digits using Dictionary
# Using map() + lambda function
res = list(map(lambda x: int(''.join(str(dig_map[int(i)]) for i in str(x))), test_list))
 
# printing result
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:




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}
 
# Using list comprehension and nested for loops
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:




# initializing list
test_list = [45, 32, 87, 34, 21, 91]
 
# initializing digit mapping
dig_map = {1 : 4, 4 : 2, 3 : 8, 2 : 6, 7 : 5, 9 : 3, 8 : 9, 5 : 7}
 
# Substitute digits using Dictionary
# Using bitwise operations
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]
 
# printing result
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 as np
 
# initializing list
test_list = [45, 32, 87, 34, 21, 91]
 
# printing original list
print("The original list is : ", test_list)
 
# initializing digit mapping
dig_map = {1: 4, 4: 2, 3: 8, 2: 6, 7: 5, 9: 3, 8: 9, 5: 7}
 
# Define vectorized function using np.vectorize()
vfunc = np.vectorize(lambda x: int(''.join(str(dig_map[int(i)]) for i in str(x))))
 
# Apply vectorized function to array
arr = np.array(test_list)
res = vfunc(arr)
 
# Convert resulting array into list
res = res.tolist()
 
# printing result
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


Article Tags :