python oneline创建给定顺序的矩阵 [英] python oneline to create matrix of given order
本文介绍了python oneline创建给定顺序的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找python oneliner,它将以给定的顺序创建矩阵并填充从1到n的值. n = r X c
I am looking for python oneliner that will create matrix with the given order and fill values from 1 to n. n = r X c
直到现在我都实现了,
Matrix1 = [[x+1 for x in range(rowCount)] for x in range(columnCount)]
print Matrix1
但是输出是
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
推荐答案
>>> matrix1 = [[1+x+y*rowCount for x in range(rowCount)] for y in range(columnCount)]
>>> matrix1
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
这篇关于python oneline创建给定顺序的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文