Python | Tuple XOR operation
Last Updated :
28 Apr, 2023
Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise XOR operation across tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using zip() + generator expression The combination of above functions can be used to perform this task. In this, we perform the task of XOR using generator expression and mapping index of each tuple is done by zip().
Python3
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
print ("The original tuple 1 : " + str (test_tup1))
print ("The original tuple 2 : " + str (test_tup2))
res = tuple (ele1 ^ ele2 for ele1, ele2 in zip (test_tup1, test_tup2))
print ("The XOR tuple : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples. The XOR operation between two elements takes O(1) time and is performed n times, so the overall time complexity is O(n).
Auxiliary Space: O(n), where n is the length of the result tuple. The generator expression creates a new tuple with the XORed elements, so the space complexity is proportional to the size of the result tuple.
Method #2: Using map() + xor The combination of above functionalities can also perform this task. In this, we perform the task of extending logic of XOR using xor and mapping is done by map().
Python3
from operator import xor
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
print ("The original tuple 1 : " + str (test_tup1))
print ("The original tuple 2 : " + str (test_tup2))
res = tuple ( map (xor, test_tup1, test_tup2))
print ("The XOR tuple : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples. This is because the map() function and the xor operator iterate through the tuples once.
Auxiliary space: O(n), where n is the length of the tuples. This is because the tuple created by the map() function is of the same length as the input tuples.
Method #3 : Using numpy
Note: install numpy module using command “pip install numpy”
Python3
import numpy as np
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = np.bitwise_xor(test_tup1,test_tup2)
print ( "The XOR tuple : " + str ( tuple (res)))
|
Output:
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using for loops
Python3
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = []
for i in range ( 0 , len (test_tup1)):
res.append(test_tup1[i]^test_tup2[i])
res = tuple (res)
print ( "The XOR tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using List Comprehension
Use list comprehension to iterate over the elements of both tuples and perform the XOR operation. The resulting list can then be converted to a tuple.
Below is the implementation of the above idea:
Python3
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
res = tuple ([test_tup1[i] ^ test_tup2[i] for i in range ( len (test_tup1))])
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
print ( "The XOR tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2.
Auxiliary Space: O(n)
Method #6: Using itertools.starmap() + operator.xor()
The itertools.starmap() function applies a function to a sequence of argument tuples, and operator.xor() function returns the bitwise XOR of two integers.
Python3
import operator
import itertools
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
res = tuple (itertools.starmap(operator.xor, zip (test_tup1, test_tup2)))
print ( "The XOR tuple : " + str (res))
|
Output
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2.
Auxiliary Space: O(n)
Method #7:Using the pandas module
- Import the pandas module and initialize two tuples.
- Create two data frames using the pandas.DataFrame() function, one for each tuple.
- Use the pandas.DataFrame.diff() function to calculate the difference between the two data frames.
- Use the pandas.DataFrame.gt() function to create a boolean mask indicating where the values in the first data frame are greater than those in the second data frame.
- Use the pandas.DataFrame.where() function to apply the boolean mask to the results of step 3.
- Use the pandas.DataFrame.fillna() function to fill any NaN values in the resulting data frame with False.
- Convert the resulting data frame to a list of lists using the pandas.DataFrame.values.tolist() function.
Python3
import pandas as pd
test_tup1 = ( 10 , 4 , 6 , 9 )
test_tup2 = ( 5 , 2 , 3 , 3 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
df1 = pd.DataFrame( list (test_tup1)).T
df2 = pd.DataFrame( list (test_tup2)).T
res_df = df1.astype( int ). apply ( lambda x: x^df2.astype( int ).iloc[ 0 ], axis = 1 )
res = tuple (res_df.iloc[ 0 ].tolist())
print ( "The XOR tuple : " , res)
|
Output-
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
The time complexity of this solution is O(n), where n is the length of the tuple.
The space complexity of this solution is O(n), as we are creating a new tuple of length n to store the result.
Please Login to comment...