【发布时间】:2020-09-15 22:50:34
【问题描述】:
我是 python 新手,我想知道如何在 python 中创建一个数字列表
number = 123456
list_to_be_created = [1,2,3,4,5,6]
提前谢谢你
【问题讨论】:
-
list(map(int, str(number)))>>[1, 2, 3, 4, 5, 6]
我是 python 新手,我想知道如何在 python 中创建一个数字列表
number = 123456
list_to_be_created = [1,2,3,4,5,6]
提前谢谢你
【问题讨论】:
list(map(int, str(number))) >> [1, 2, 3, 4, 5, 6]
>>> list(range(1, 7))
[1, 2, 3, 4, 5, 6]
如果您确实有数字 123456(即 123456 的 int 值)并且您想将其转换为数字列表,您可以做:
>>> number = 123456
>>> list_to_be_created = list(map(int, str(number)))
>>> list_to_be_created
[1, 2, 3, 4, 5, 6]
【讨论】:
您可以尝试使用列表推导:
number = 123456
print([int(x) for x in str(number)])
返回:
[1, 2, 3, 4, 5, 6]
【讨论】: