Skip to main content
 首页 » 编程设计

python之如何将一串数字转换为一位整数列表

2025年05月04日10zhoujg

str(reduce(lambda x, y: x * y, range(1, 11))) -> this returns 10! or '3628800' 

在不创建 for 循环的情况下,如何将此函数映射到另一个函数作为一位整数列表:例如reduce(lambda x, y: x+ y, [3, 6, 2, 8, 8, 0, 0])

澄清:

reduce(lambda x, y: x + y, [str(reduce(lambda x, y: x * y, range(1, 11)))])

如何将 [ ] 中的所有内容转换为一位数整数列表,以便第一个函数可以对其求值?当然,我需要在[ ]内部对其进行转换。

请您参考如下方法:

我认为您只是在寻找 map(int, s),其中 s 是您想要从中获取整数的字符串。例如:

>>> import math 
>>> math.factorial(10) 
3628800 
>>> str(math.factorial(10)) 
'3628800' 
>>> map(int,str(math.factorial(10))) 
[3, 6, 2, 8, 8, 0, 0] 
>>> sum(map(int,str(math.factorial(10)))) 
27