How to concatenate two integers in Python?

前端 未结 14 1908
南旧
南旧 2020-12-15 03:51

How do I concatenate two integer numbers (for example: 10 and 20) in Python to get a returned value of 1020?

相关标签:
14条回答
  • 2020-12-15 04:19

    Using Math converter is faster than converting to string and back in my testing:

    In [28]: fn = lambda x, y: x*10 + y                                                                               
    
    In [29]: timeit fn(1,2)                                                                                            
    88.4 ns ± 1.26 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    In [30]: timeit int(str(1) + str(2))                                                                               
    427 ns ± 11.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    
    0 讨论(0)
  • 2020-12-15 04:21

    Example 1: (Example 2 is much faster, don't say I didn't warn you!)

    a = 9
    b = 8
    def concat(a, b):
        return eval(f"{a}{b}")
    

    Example:

    >>> concat(a, b)
    98
    

    Example 2:

    For people who think eval is 'evil', here's another way to do it:

    a = 6
    b = 7
    def concat(a, b):
        return int(f"{a}{b}")
    

    Example:

    >>> concat(a, b)
    67
    

    EDIT:

    I thought it would be convienient to time these codes, look below:

    >>> min(timeit.repeat("for x in range(100): int(str(a) + str(b))", "",
              number=100000, globals = {'a': 10, 'b': 20}))
    9.107237317533617
    >>> min(timeit.repeat("for x in range(100): int(f'{a}{b}')", "",
              number=100000, globals = {'a': 10, 'b': 20}))
    6.4986298607643675
    >>> min(timeit.repeat("for x in range(5): eval(f'{a}{b}')", "", #notice the range(5) instead of the range(100)
              number=100000, globals = {'a': 10, 'b': 20}))
    4.089137231865948 #x20
    

    The times:

    eval: about 1 minute and 21 seconds.
    
    original answer: about 9 seconds.
    
    my answer: about 6 and a half seconds.
    

    Conclusion:

    The original answer does look more readable, but if you need a good speed, choose int(f'{vara}{varb}')

    P.S: My int(f'{a}{b}) syntax only works on python 3.6+, as the f'' syntax is undefined at python versions 3.6-

    0 讨论(0)
  • 2020-12-15 04:24

    Here's another way of doing it:

    a = 10
    b = 20
    
    x = int('{}{}'.format(a, b))
    
    0 讨论(0)
  • 2020-12-15 04:27

    Cast both to a string, concatenate the strings and then cast the result back to an integer:

    z = int(str(x) + str(y))
    
    0 讨论(0)
  • 2020-12-15 04:27

    Using math is probably faster than solutions that convert to str and back:

    If you can assume a two digit second number:

    def f(x, y):
        return x*100+y
    

    Usage:

    >>> f(1,2)
    102
    >>> f(10,20)
    1020
    

    Although, you probably would want some checks included to verify the second number is not more than two digits. Or, if your second number can be any number of digits, you could do something like this:

    import math
    def f(x, y):
        a = math.floor(math.log10(y))
        return int(x*10**(1+a)+y)
    

    Usage:

    >>> f(10,20)
    1020
    >>> f(99,193)
    99193
    

    This version however, does not allow you to merge numbers like 03 and 02 to get 0302. For that you would need to either add arguments to specify the number of digits in each integer, or use strings.

    0 讨论(0)
  • 2020-12-15 04:29

    A nice way as well would be to use the built-in reduce() function:

    reduce(lambda x,y:x*10+y,[10,20])
    
    0 讨论(0)
提交回复
热议问题