user1320217
user1320217

Reputation: 121

Python convert Tuple to Integer

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

Example:

input = (1, 3, 7)

output = 137

Upvotes: 11

Views: 35348

Answers (8)

22nds
22nds

Reputation: 166

The simplest method to change a tuple into a number is to use string formating.

input = (1, 3, 7)
output = int('{}{}{}'.format(input[0], input[1], input[2]))

# TEST
print(output) # 137
print(type(output)) # <class 'int'>

Upvotes: 1

Marcin
Marcin

Reputation: 49816

How about:

In [19]: filter(set('0123456789').__contains__,str((1,2,3)))
Out[19]: '123'

I believe this is the simplest solution.

A very fast solution is:

plus=ord("0").__add__ # separate out for readability; bound functions are partially-applied functions
int(str(bytearray(map(plus,x)))) #str is necessary

How that stacks up against the next-fastest solution:

$ python -m timeit -s 'x=tuple(list(range(1,10))*10)' 'plus=ord("0").__add__;int(str(bytearray(map(plus,x))))'
10000 loops, best of 3: 47.7 usec per loop

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

Upvotes: 1

Rik Poggi
Rik Poggi

Reputation: 29302

@Marcin bytearray 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/pass
int(bytearray(map(plus, x)).decode())       9.85 usec/pass
int(''.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/pass
int(bytes(map(plus, x)).decode())           7.80 usec/pass
int(bytearray(map(plus,x)).decode())        7.99 usec/pass
int(''.join(map(str, x)))                   17.46 usec/pass
reduce(lambda rst, d: rst * 10 + d, x)      19.03 usec/pass

Judge by yourselves :)

Upvotes: 1

Gareth Latty
Gareth Latty

Reputation: 88977

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 for pos, val in enumerate(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.

Upvotes: 4

User
User

Reputation: 14853

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

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

this is a for-loop in one line.

Upvotes: 1

Abhijit
Abhijit

Reputation: 63707

Just another way to do it

>>> sum(n*10**i for (i,n) in enumerate(input[::-1]))
137

and yet another

>>> int(str(input).translate(None,'(,) '))
137

Upvotes: 0

yazu
yazu

Reputation: 4650

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

Upvotes: 25

jamylak
jamylak

Reputation: 133514

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

Upvotes: 17

Related Questions