如何将整数拆分为数字列表?

问题描述 投票:0回答:11

假设我有一个输入整数

12345
。我怎样才能将它分成像
[1, 2, 3, 4, 5]
这样的列表?

python
11个回答
194
投票

将数字转换为字符串,以便您可以对其进行迭代,然后将每个数字(字符)转换回列表理解中的int

>>> [int(i) for i in str(12345)] [1, 2, 3, 4, 5]
    

119
投票
以字符串形式返回数组

>>> list(str(12345)) ['1', '2', '3', '4', '5']

以整数形式返回数组

>>> map(int,str(12345)) [1, 2, 3, 4, 5]
    

14
投票
我不想将整数转换为字符串,所以这是我为此使用的函数:

def digitize(n, base=10): if n == 0: yield 0 while n: n, d = divmod(n, base) yield d

示例:

tuple(digitize(123456789)) == (9, 8, 7, 6, 5, 4, 3, 2, 1) tuple(digitize(0b1101110, 2)) == (0, 1, 1, 1, 0, 1, 1) tuple(digitize(0x123456789ABCDEF, 16)) == (15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

如您所见,这将产生从右到左的数字。如果您想要从左到右的数字,您需要创建一个序列,然后反转它:

reversed(tuple(digitize(x)))

您还可以在分割整数时使用此函数进行基数转换。以下示例将十六进制数拆分为二进制半字节作为元组:

import itertools as it tuple(it.zip_longest(*[digitize(0x123456789ABCDEF, 2)]*4, fillvalue=0)) == ((1, 1, 1, 1), (0, 1, 1, 1), (1, 0, 1, 1), (0, 0, 1, 1), (1, 1, 0, 1), (0, 1, 0, 1), (1, 0, 0, 1), (0, 0, 0, 1), (1, 1, 1, 0), (0, 1, 1, 0), (1, 0, 1, 0), (0, 0, 1, 0), (1, 1, 0, 0), (0, 1, 0, 0), (1, 0, 0, 0))

请注意,此方法不处理小数,但可以进行调整。


10
投票
[int(i) for i in str(number)]

或者,如果不想使用列表理解或者您想使用不同于 10 的基数

from __future__ import division # for compatibility of // between Python 2 and 3 def digits(number, base=10): assert number >= 0 if number == 0: return [0] l = [] while number > 0: l.append(number % base) number = number // base return l
    

4
投票
虽然

list(map(int, str(x)))

 是 Pythonic 方法,但您可以制定逻辑来派生数字而无需任何类型转换:

from math import log10 def digitize(x): n = int(log10(x)) for i in range(n, -1, -1): factor = 10**i k = x // factor yield k x -= k * factor res = list(digitize(5243)) [5, 2, 4, 3]

生成器的一个好处是您可以无缝地提供

set

tuple
next
 等,无需任何额外的逻辑。


2
投票
就像@nd所说,但使用 int 的内置函数转换为不同的基数

>>> [ int(i,16) for i in '0123456789ABCDEF' ] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] >>> [int(i,2) for i in "100 010 110 111".split()] [4, 2, 6, 7]
    

0
投票
另一种不涉及字符串转换的解决方案:

from math import log10 def decompose(n): if n == 0: return [0] b = int(log10(n)) + 1 return [(n // (10 ** i)) % 10 for i in reversed(range(b))]
    

0
投票
使用字符串的

join

split
 方法:

>>> a=12345 >>> list(map(int,' '.join(str(a)).split())) [1, 2, 3, 4, 5] >>> [int(i) for i in ' '.join(str(a)).split()] [1, 2, 3, 4, 5] >>>
这里我们还使用 

map

 或列表理解来获取列表。


0
投票
使用

str

 和列表理解是最具可读性和最快的方法:

def int_to_list(): q = 1111111111111122222222222222222333333333333333333333 res = [] while q != 0: res.append(q % 10) q //= 10 return res[::-1] print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
0.1116315030012629
def int_to_list():
    q = 1111111111111122222222222222222333333333333333333333
    return [int(x) for x in str(q)]

print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
0.10781958399820724
    

-1
投票
字符串与数组一样可迭代,因此只需将其转换为字符串即可:

str(12345)
    

-4
投票
简单地把它变成一个字符串,分割,然后把它变回一个数组整数:

nums = [] c = 12345 for i in str(c): l = i.split()[0] nums.append(l) np.array(nums)
    
© www.soinside.com 2019 - 2024. All rights reserved.