Converting integer to digit list

前端 未结 10 1083
渐次进展
渐次进展 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-11-30 23:51

    If you have a string like this: '123456' and you want a list of integers like this: [1,2,3,4,5,6], use this:

    >>>s = '123456'    
    >>>list1 = [int(i) for i in list(s)]
    >>>print(list1)
    
    [1,2,3,4,5,6]
    

    or if you want a list of strings like this: ['1','2','3','4','5','6'], use this:

    >>>s = '123456'    
    >>>list1 = list(s)
    >>>print(list1)
    
    ['1','2','3','4','5','6']
    
    0 讨论(0)
  • 2020-11-30 23:54

    There are already great methods already mentioned on this page, however it does seem a little obscure as to which to use. So I have added some mesurements so you can more easily decide for yourself:


    A large number has been used (for overhead) 1111111111111122222222222222222333333333333333333333

    Using map(int, str(num)):

    import timeit
    
    def method():
        num = 1111111111111122222222222222222333333333333333333333
        return map(int, str(num))
    
    print(timeit.timeit("method()", setup="from __main__ import method", number=10000)
    

    Output: 0.018631496999999997

    Using list comprehension:

    import timeit

    def method():
        num = 1111111111111122222222222222222333333333333333333333
        return [int(x) for x in str(num)]
    
    print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
    

    Output: 0.28403817900000006

    Code taken from this answer

    The results show that the first method involving inbuilt methods is much faster than list comprehension.

    The "mathematical way":

    import timeit
    
    def method():
        q = 1111111111111122222222222222222333333333333333333333
        ret = []
        while q != 0:
            q, r = divmod(q, 10) # Divide by 10, see the remainder
            ret.insert(0, r) # The remainder is the first to the right digit
        return ret
    
    print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
    

    Output: 0.38133582499999996

    Code taken from this answer

    The list(str(123)) method (does not provide the right output):

    import timeit
    
    def method():
        return list(str(1111111111111122222222222222222333333333333333333333))
        
    print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
    

    Output: 0.028560138000000013

    Code taken from this answer

    The answer by Duberly González Molinari:

    import timeit
    
    def method():
        n = 1111111111111122222222222222222333333333333333333333
        l = []
        while n != 0:
            l = [n % 10] + l
            n = n // 10
        return l
    
    print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
    

    Output: 0.37039988200000007

    Code taken from this answer

    Remarks:

    In all cases the map(int, str(num)) is the fastest method (and is therefore probably the best method to use). List comprehension is the second fastest (but the method using map(int, str(num)) is probably the most desirable of the two.

    Those that reinvent the wheel are interesting but are probably not so desirable in real use.

    0 讨论(0)
  • 2020-11-30 23:54
    n = int(raw_input("n= "))
    
    def int_to_list(n):
        l = []
        while n != 0:
            l = [n % 10] + l
            n = n // 10
        return l
    
    print int_to_list(n)
    
    0 讨论(0)
  • 2020-11-30 23:59
    num = list(str(100))
    index = len(num)
    while index > 0:
        index -= 1
        num[index] = int(num[index])
    print(num)
    

    It prints [1, 0, 0] object.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-12-01 00:12

    Use list on a number converted to string:

    In [1]: [int(x) for x in list(str(123))]
    Out[2]: [1, 2, 3]
    
    0 讨论(0)
提交回复
热议问题