Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert Tuple to Integer

Is there any function that can convert a tuple into an integer?

Example:

input = (1, 3, 7)

output = 137
like image 605
user1320217 Avatar asked Apr 08 '12 12:04

user1320217


People also ask

How do you convert a tuple in Python?

Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.

How do you convert to int in Python?

To convert a string to integer in Python, use the int() function. This function takes two parameters: the initial string and the optional base to represent the data. Use the syntax print(int("STR")) to return the str as an int , or integer.

Can we change value in tuple Python?

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.


2 Answers

>>> reduce(lambda rst, d: rst * 10 + d, (1, 2, 3))
123
like image 156
yazu Avatar answered Oct 21 '22 18:10

yazu


>>> x = (1,3,7)
>>> int(''.join(map(str,x)))
137
like image 24
jamylak Avatar answered Oct 21 '22 18:10

jamylak