首页 > 解决方案 > 在python中将整数转换为空格分隔的列表

问题描述

我的输入为:

第一行包含两个空格分隔的整数 N 和 M。然后在下一行是矩阵 A 的 NxM 输入: 输入:

4 5
11110
11010
11000
00000

我希望输出为列表列表(整数)

输出:

[[1, 1, 1, 1, 0],
[1, 1, 0, 1, 0], 
[1, 1, 0, 0, 0], 
[0, 0, 0, 0, 0]]

标签: pythonpython-3.xlistarraylistinput

解决方案


将输入读取为字符串。然后您可以使用此代码段获取输出

op_list = []
def to_list(number):
    return [int(dig) for dig in number]

op_list.append(to_list("11110"))
op_list.append(to_list("11010"))
op_list.append(to_list("11000"))
op_list.append(to_list("00000"))

print(op_list)

推荐阅读