如何在 Python 中连接两个整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12838549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:57:59  来源:igfitidea点击:

How to concatenate two integers in Python?

pythonconcatenation

提问by Fi3n1k

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

如何在 Python 中连接两个整数(例如:10 和 20)以获得返回值1020

采纳答案by vonPetrushev

The best way to do this in python was given in the accepted answer - but if you want to do this in jinja2 templates - the concatenation operator ~gives you a neat way of doing this since it looks for the unicode representation of all objects, thus, you can 'concatenate integers' as well.

在接受的答案中给出了在 python 中执行此操作的最佳方法-但如果您想在 jinja2 模板中执行此操作-连接运算符~为您提供了一种简洁的方法,因为它会查找所有对象的 unicode 表示,因此,您也可以“连接整数”。

That is you can do this (given a=10and b=20):

那就是你可以这样做(给定a=10b=20):

{{ a ~ b }}

回答by Konstantin Dinev

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

将两者都转换为字符串,连接字符串,然后将结果转换回整数:

z = int(str(x) + str(y))

回答by irrenhaus3

A rough but working implementation:

一个粗略但有效的实现:

i1,i2 = 10,20
num = int('%i%i' % (i1,i2))

Basically, you just merge two numbers into one string and then cast that back to int.

基本上,您只需将两个数字合并为一个字符串,然后将其转换回 int。

回答by Corey Goldberg

using old-style string formatting:

使用旧式字符串格式:

>>> x = 10
>>> y = 20
>>> z = int('%d%d' % (x, y))
>>> print z
1020

回答by Matt

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

使用数学可能比转换为 str 并返回的解决方案更快:

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.

然而,这个版本不允许你合并像 03 和 02 这样的数字来得到 0302。为此你需要添加参数来指定每个整数中的位数,或者使用字符串。

回答by Gareth Latty

Just to give another solution:

只是给出另一个解决方案:

def concat_ints(a, b):
    return a*(10**len(str(b)))+b

>>> concat_ints(10, 20)
1020

回答by zenpoy

Of course the 'correct' answer would be Konstantin's answer. But if you still want to know how to do it without using string casts, just with math:

当然,“正确”的答案是康斯坦丁的答案。但是如果你仍然想知道如何在不使用字符串强制转换的情况下做到这一点,只需使用数学:

import math

def numcat(a,b):
    return int(math.pow(10,(int(math.log(b,10)) + 1)) * a + b)

>> numcat(10, 20)
>> 1020

回答by arulmr

Using this function you can concatenate as many numbers as you want

使用此功能,您可以连接任意数量的数字

def concat(*args):
    string = ''
    for each in args:
        string += str(each) 
    return int(string)

For example concat(20, 10, 30)will return 201030an an integer

例如concat(20, 10, 30)将返回201030一个整数

OR

或者

You can use the one line program

您可以使用单行程序

int(''.join(str(x) for x in (20,10,30)))

This will also return 201030.

这也将返回201030

回答by user3481919

def concatenate_int(x, y):

  try:
     a = floor(log10(y))
  except ValueError:
     a = 0
  return int(x * 10 ** (1 + a) + y)


def concatenate(*l):
  j = 0
  for i in list(*l):
     j = concatenate_int(j, i)
  return j

回答by Berend

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

一个不错的方法是使用内置的 reduce() 函数:

reduce(lambda x,y:x*10+y,[10,20])