添加号码而不携带

rul*_*ler 3 python math

可以说我有两个数字,

1号:

1646
Run Code Online (Sandbox Code Playgroud)

2号:

2089
Run Code Online (Sandbox Code Playgroud)

你看到从左到右添加它们没有进位加起来3625如何在python中执行此操作?我不确定但是我需要它添加9 + 6这是15但是当它添加8 + 4时没有继续1是否有任何方式在python中添加这样的?我正在寻找的最终结果是

3625
Run Code Online (Sandbox Code Playgroud)

因为它没有进行剩余的数字

FMc*_*FMc 8

这将适用于不同长度的不同N个整数:

from itertools import izip_longest

nums = [1646,
        2089,
         345]

revs = [str(n)[-1::-1] for n in nums]        # nums as reversed strings
izl  = izip_longest(*revs, fillvalue = '0')  # zip the digits together
xsum = lambda ds: str(sum(map(int, ds)))[-1] # digits -> last digit of their sum
rres = ''.join(xsum(ds) for ds in izl)       # result, but as reversed string
res  = int(rres[-1::-1])                     # result: 3960
Run Code Online (Sandbox Code Playgroud)

类似的想法,但使用map而不是izip_longest.我更喜欢这个.

revs = [str(n)[-1::-1] for n in nums]           # nums as reversed strings
dsum = lambda *ds: sum(int(d or 0) for d in ds) # str-digits -> their sum
sums = map(dsum, *revs)[-1::-1]                 # digit sums, in order
ones = [str(s % 10) for s in sums]              # last digits of the sums
res  = int(''.join(ones))                       # result: 3960
Run Code Online (Sandbox Code Playgroud)