Skip to content Skip to sidebar Skip to footer

Python Convert Tuple To Integer

Is there any function that can convert a tuple into an integer? Example: input = (1, 3, 7) output = 137

Solution 1:

>>>reduce(lambda rst, d: rst * 10 + d, (1, 2, 3))
123

Solution 2:

>>>x = (1,3,7)>>>int(''.join(map(str,x)))
137

Solution 3:

While converting it to a string then to an int works, it's a somewhat hackish method. We have all the information we need to make a number, namely:

  • The digits.
  • The position of the digits.

As we have this information, we can calculate the number by calculating the value of each unit at each position, then multiplying it up by the digit at said position. We then add together the results and we have our number. This can be done in one line like so:

test = (1, 2, 3)
sum((10**pos)*val for pos, val in enumerate(reversed(test)))

Let's break this down:

>>>list(enumerate(reversed(test)))
[(0, 3), (1, 2), (2, 1)]

So then, if we multiply it up:

>>> list((10**pos)*val for pos, val in enumerate(reversed(test)))
[3, 20, 100]

So we just sum to get 123.

Edit: A note on speed:

python -m timeit "int(''.join(map(str,(1,2,3))))"100000 loops, best of 3: 2.7 usec per loop

python -m timeit 'sum((10**pos)*val forpos, val inenumerate(reversed((1,2,3))))'
100000 loops, best of 3: 2.29 usec per loop

python -m timeit -s 'from functools import reduce' 'reduce(lambda rst, d: rst * 10 + d, (1, 2, 3))'
1000000 loops, best of 3: 0.598 usec per loop

So if you are going on speed, Andrey Yazu's answer has it. I'm torn as to what I feel is more readable. I always find lambdas ugly somehow, but in general, I think it's still the more readable method.

Edit 2: With very large tuples:

Length 20:

python -m timeit -s "x=tuple(list(range(1,10))*2)""int(''.join(map(str, x)))"100000 loops, best of 3: 5.45 usec per loop

python -m timeit -s "x=tuple(list(range(1,10))*2)""sum((10**pos)*val for pos, val in enumerate(reversed(x)))"100000 loops, best of 3: 11.7 usec per loop

python -m timeit -s "x=tuple(list(range(1,10))*2)" -s 'from functools import reduce' 'reduce(lambda rst, d: rst * 10 + d, x)'
100000 loops, best of 3: 4.18 usec per loop

Length 100:

python -m timeit -s "x=tuple(list(range(1,10))*10)""int(''.join(map(str, x)))"100000 loops, best of 3: 18.6 usec per loop

python -m timeit -s "x=tuple(list(range(1,10))*10)""sum((10**pos)*val for pos, val in enumerate(reversed(x)))"10000 loops, best of 3: 72.9 usec per loop

python -m timeit -s "x=tuple(list(range(1,10))*10)" -s 'from functools import reduce' 'reduce(lambda rst, d: rst * 10 + d, x)'
10000 loops, best of 3: 25.6 usec per loop

Here we see that the fastest method is actually the string operation - however, the reality is you are unlikely to be using this outside of the range of, say, 10 digit numbers - where the reduce() method still dominates speed-wise. I would also argue that the string method is hackish and less clear to the reader, which would normally be the priority over speed.

Solution 4:

this does not convert the integers to strings and concats them:

>>>sum([(10 ** i) * input[len(input)-i-1] for i inrange(len(input))])
123

this is a for-loop in one line.

Solution 5:

@Marcinbytearray solution is indeed the fastest one in Python 2.

Following the same line in Python 3 one could do:

>>>plus = ord("0").__add__>>>int(bytes(map(plus, x)))

Python 3 handles string and bytes in a different way than Python 2, so in order to understand better the situation I did a little timings. The following are the results I got on my machine.

With Python 2.7 (code):

int(str(bytearray(map(plus, x))))           8.40 usec/passint(bytearray(map(plus, x)).decode())       9.85 usec/passint(''.join(map(str, x)))                   11.97 usec/pass
reduce(lambda rst, d: rst * 10 + d, x)      22.34 usec/pass

While with Python 3.2 (code):

int(bytes(map(plus, x)))                    7.10 usec/passint(bytes(map(plus, x)).decode())           7.80 usec/passint(bytearray(map(plus,x)).decode())        7.99 usec/passint(''.join(map(str, x)))                   17.46 usec/pass
reduce(lambda rst, d: rst * 10 + d, x)      19.03 usec/pass

Judge by yourselves :)

Post a Comment for "Python Convert Tuple To Integer"