[英]Python: how can I transform an array in a matrix with N columns, that are the same array repeated N times?
I have an array and I have to create a matrix with N columns (N is a parameter that can be changed).我有一个数组,我必须创建一个包含 N 列的矩阵(N 是一个可以更改的参数)。 So we have to copy this array N times.
所以我们必须复制这个数组 N 次。
If I understand correctly your input would be for example:如果我理解正确,您的输入将是例如:
a = [12, 8, 9]
n = 2
And the output would be:输出将是:
b = [[12, 12],
[ 8, 8],
[ 9, 9]]
Here is how you could do that:您可以这样做:
b = [[i]*n for i in a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.