如何将列表组合成一个数字/如何将数字拆分为列表

如何将列表里面的数字,合并成一个数字:

使用reduce(function,list)

from functools import reduce

x = [1,2,3,4,5]
combine = reduce((lambda x,y : x*10 + y),x)
print(combine)
#result: 12345

如何将一个数字, 拆分为单个数字的列表:

使用map(function,iterable)

num = 12345
x = list(map(int,str(num)))
print(x)
#result: [1,2,3,4,5]

猜你喜欢

转载自blog.csdn.net/weixin_42317507/article/details/84189682