且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将整数转换为数字列表

更新时间:2023-02-10 17:39:32

首先将整数转换为字符串,然后使用map在其上应用int:

Convert the integer to string first, and then use map to apply int on it:

>>> num = 132
>>> map(int, str(num))    #note, This will return a map object in python 3.
[1, 3, 2]

或使用列表理解:

>>> [int(x) for x in str(num)]
[1, 3, 2]