python - 得到结果后编辑输出

标签 python python-2.7

编写一个程序,以 2 位数字 X、Y 作为输入并生成一个二维数组。数组第i行j列的元素值应为i*j。 注:i=0,1..,X-1; j=0,1,¡Y-1。

示例

假设向程序提供以下输入: 3,5 那么,程序的输出应该是: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

我的程序:

x=int(raw_input())
y=int(raw_input())

l=[]

for i in range(x):    

   for j in range(y):
         l.append((i*j))
print l

我的输出:

[0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 2, 4, 6, 8]

我无法进行最后的接触,例如:

[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] 

有人可以建议一下吗?谢谢。

最佳答案

x = int(raw_input())
y = int(raw_input())

l=[]
for i in range(x):   
    inner_list = []
    for j in range(y):
        inner_list.append((i*j))
    l.append(inner_list)

关于python - 得到结果后编辑输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777099/

相关文章:

python - Pandas:将分组的 df 转换为以两列作为键、值对的字典列表

python - 将数据填充到矩阵中,传递其定义的索引遇到 IndexError 的元素

python - Numpy 最大池化卷积

python - 正则表达式:字符类中的\Z

python - 在 Django 中处理联系表,未发送电子邮件

python - output_notebook() 未定义 - 为什么?

mysql - 错误 "TypeError: not all arguments converted during string formatting"MySQLdb

python-2.7 - Pandas - 使用自定义日历获取日期之间的工作日?

Python 多处理通过 argparse 和 pyinstaller 抛出错误

php - 将字符串从 html 文件中的表单传递到符合 utf-8 编码的 python 脚本