Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Convert Tuple to integer

Python | Convert Tuple to integer

Sometimes, while working with records, we can have a problem in which we need to convert the data records to integer by joining them. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using reduce() + lambda 

The combination of above functions can be used to perform this task. In this, we use lambda function to perform logic of conversion and reduce performs task of iteration and combining result. 

Python3




# Python3 code to demonstrate working of
# Convert Tuple to integer
# Using reduce() + lambda
import functools
 
# initialize tuple
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer
# Using reduce() + lambda
res = functools.reduce(lambda sub, ele: sub * 10 + ele, test_tuple)
 
# printing result
print("Tuple to integer conversion : " + str(res))


Output : 

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Time complexity: O(n)
Auxiliary space: O(1)

Method #2 : Using int() + join() + map() 

The combination of these functions can also be used to perform this task. In this, we convert each element to string using join() and iterate using map(). At last we perform integer conversion. 

Python3




# Python3 code to demonstrate working of
# Convert Tuple to integer
# Using int() + join() + map()
 
# initialize tuple
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer
# Using int() + join() + map()
res = int(''.join(map(str, test_tuple)))
 
# printing result
print("Tuple to integer conversion : " + str(res))


Output : 

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Time complexity: O(n), where n is the length of the input tuple.
O(k), where k is the length of the resulting string.

Method #3: Using str() and int() methods

Python3




# Python3 code to demonstrate working of
# Convert Tuple to integer
 
# initialize tuple
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer
res=""
for i in test_tuple:
    res+=str(i)
res=int(res)
 
# printing result
print("Tuple to integer conversion : " + str(res))


Output

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Method #4: Using in operator + end() function

Python3




test_tuple=(1,4,5)
for i in test_tuple:
  print(i,end="")


Output

145

Method #5 : Using regular expressions (re module)
This method uses the re module to match the elements of the tuple as individual digits and then join them using the re.sub() method. The resulting string is then converted to an integer using the int() method.

Python3




import re
 
test_tuple = (1, 4, 5)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Convert Tuple to integer using regular expressions
res = int(re.sub(r'\D', '', ''.join(map(str, test_tuple))))
 
# printing result
print("Tuple to integer conversion : " + str(res))
 
#this code is contributed by edula vinay kumar reddy


Output

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Time complexity: O(n)
Auxiliary Space: O(n)

Method#6: Using Recursive method.

The algorithm of the tuple_to_int() function is as follows:

  1. If the length of the tuple is 1, return the only element in the tuple.
  2. Otherwise, take the first element of the tuple and multiply it by 10 to the power of the length of the tuple minus 1.
  3. Add the result to the recursive call to tuple_to_int() with the remaining elements of the tuple.
  4. Repeat until the base case is reached.

Python3




def tuple_to_int(tup):
    if len(tup) == 1:
        return tup[0]
    else:
        return tup[0] * (10 ** (len(tup) - 1)) + tuple_to_int(tup[1:])
 
test_tuple = (1, 4, 5)
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
res = tuple_to_int(test_tuple)
# printing result
print("Tuple to integer conversion : " + str(res))
  
#this code is contributed by tvsk.


Output

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145

Time complexity: O(n), where n is the length of the tuple, because it needs to process each element of the tuple exactly once. 
Auxiliary space: O(n), because the function call stack can have at most n function, calls in it at any given time.

Method #7: Using math.pow() + reversed()

  • Create a function tuple_to_int that takes a tuple as input.
  • Use the enumerate() function along with the reversed() function to iterate through the tuple in reverse order and assign an index to each element.
  • For each element in the reversed tuple, calculate the power of 10 to the index using the math.pow() function, and multiply it with the element.
  • Use the sum() function to add up all the calculated values to get the final integer value.
  • Create a tuple test_tuple with values (1, 4, 5).
  • Print the original tuple using the print() function.
  • Call the tuple_to_int() function with test_tuple as the argument and assign the result to a variable res.
  • Print the result using the print() function.

Python3




import math
 
def tuple_to_int(tup):
    return sum(math.pow(10, i) * num for i, num in enumerate(reversed(tup)))
 
test_tuple = (1, 4, 5)
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
res = tuple_to_int(test_tuple)
# printing result
print("Tuple to integer conversion : " + str(res))


Output

The original tuple : (1, 4, 5)
Tuple to integer conversion : 145.0

Time complexity: O(n)
Auxiliary space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments