如何在Python中有效地重复矩阵中的二进制模式和比率?
我想在python中有效打印一个矩阵,该矩阵遵循5 0s的列中的特定模式,然后在3 1s,然后在5 0s等上等等,依此类推,如下所示,如下所示:1000行:
0000
0000
0000
0000
0000
1111
1111
1111
0000
0000
0000
0000
0000
1111
1111
1111
...
I want to efficiently print a matrix in python that follows a specific pattern in the columns of 5 0s then 3 1s then 5 0s and so on and so forth as shown below for 1000 rows:
0000
0000
0000
0000
0000
1111
1111
1111
0000
0000
0000
0000
0000
1111
1111
1111
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
np.block
的组合和列表理解:可以将其功能化以回答类似的问题类似的问题:
You can use a combination of
np.block
and list comprehension:This can be functionalized to answer similar questions like so:
您可以做到这一点:
您可以检查形状。它有125行8行IE 1000:
要打印它可以使用:
输出:
Here's how you can do it:
You can check the shape. It has 125 rows of 8 rows i.e. 1000:
To print it you can use:
Output:
这没有使用
numpy
,但它可以完成工作。您以后可以使用numpy.matrix
将其转换为numpy矩阵
。输出 -
This didn't use
numpy
but it does the job. You can later convert it into anumpy matrix
usingnumpy.matrix
.Output -
本着@ishan高尔夫的精神,这里是一行,没有图书馆:
In the spirit of @Ishan golfing this, here it is in one line, no librarys:
使用
np.tile
,您甚至可以避免使用列表理解:Use
np.tile
, you can even avoid using list comprehension: