Python convert Tuple to Integer

前端 未结 8 580
心在旅途
心在旅途 2021-01-02 03:10

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

Example:

input = (1, 3, 7)

output = 137
相关标签:
8条回答
  • 2021-01-02 03:33
    >>> x = (1,3,7)
    >>> int(''.join(map(str,x)))
    137
    
    0 讨论(0)
  • 2021-01-02 03:35

    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.

    0 讨论(0)
  • 2021-01-02 03:35

    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
    
    0 讨论(0)
  • 2021-01-02 03:37

    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.

    0 讨论(0)
  • 2021-01-02 03:42

    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
    
    0 讨论(0)
  • 2021-01-02 03:44

    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'>
    
    0 讨论(0)
提交回复
热议问题