Converting integer to digit list

前端 未结 10 1117
渐次进展
渐次进展 2020-11-30 23:45

What is the quickest and cleanest way to convert an integer into a list?

For example, change 132 into [1,3,2] an

10条回答
  •  我在风中等你
    2020-12-01 00:06

    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]
    

    or using a list comprehension:

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

提交回复
热议问题