如何有效地创建具有特定模式的二进制矩阵?
如何在Python中有效地创建此交替模式的二进制表?如下所示,1s重复4个元素,然后重复另外4个元素,依此类推,依此类推:
101010
101010
101010
101010
010101
010101
010101
010101
101010
101010
...
How can I create a binary table of this alternating pattern efficiently in python? The 1s repeat for 4 elements then 0s for another 4 elements and so on and so forth as shown below:
101010
101010
101010
101010
010101
010101
010101
010101
101010
101010
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑使用
numpy.tile
Consider using
numpy.tile
使用列表理解:
您可能更喜欢使用0B101010和0B010101而不是字符串,但结果打印为42和21。
Using list comprehension:
You may prefer use 0b101010 and 0b010101 instead of string, but the result printed will be 42 and 21.
您还可以使用
np.resize
,np.repeat
和np.eye
为此:np.resize
采用图案并将其朝目标尺寸复制到目标尺寸。You can also use
np.resize
,np.repeat
, andnp.eye
for this:np.resize
takes a pattern and copies it in every direction out to the target size.