[英]How do I repeat binary pattern and ratio in matrix effectively in python?
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:我想在 python 中高效地打印一个矩阵,该矩阵遵循 5 个 0 列中的特定模式,然后是 3 个 1,然后是 5 个 0,依此类推,如下所示 1000 行:
0000
0000
0000
0000
0000
1111
1111
1111
0000
0000
0000
0000
0000
1111
1111
1111
...
You can use a combination of np.block
and list comprehension:您可以结合使用np.block
和列表理解:
out = np.block([[np.zeros((5, 4))],[np.ones((3, 4))]] * 125).astype(int)
This can be functionalized to answer similar questions like so:这可以被功能化以回答类似的问题,如下所示:
def block_repeat(size, *args):
block = np.block([[a] for a in args])
return np.resize(block, (size,) + block.shape[1:])
block_repeat(1000, np.zeros((5,4)), np.ones((3,4)))
Out[]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
...,
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
This didn't use numpy
but it does the job.这没有使用numpy
但它完成了工作。 You can later convert it into a numpy matrix
using numpy.matrix
.您稍后可以使用numpy.matrix
将其转换为numpy matrix
。
import itertools
cycle = itertools.cycle([("0") for i in range(5)] + [("1") for i in range(3)])
for i in range(1000):
item = next(cycle)
print(4 * item)
Output - Output -
00000
00000
00000
00000
00000
11111
11111
11111
00000
00000
00000
00000
00000
11111
11111
11111
Here's how you can do it:以下是您的操作方法:
import numpy as np
my_array = np.array([[[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [1,1,1,1], [1,1,1,1], [1,1,1,1]] for i in range(125)])
You can check the shape.你可以检查形状。 It has 125 rows of 8 rows ie 1000:它有 125 行 8 行即 1000:
>>> my_array.shape
(125, 8, 4)
To print it you can use:要打印它,您可以使用:
count_row = 0
for row in my_array:
for row2 in row:
print(row2)
count_row += 1
Output: Output:
# count_row is equal to 1000.
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[0 0 0 0]
....
In the spirit of @Ishan golfing this, here it is in one line, no librarys:本着@Ishan 打高尔夫球的精神,这里是一行,没有图书馆:
print("\n".join(["00000111"[i % 8] * 4 for i in range(1000)]))
# Explanation
pattern = "00000111"
for i in range(1000):
index = i % len(pattern)
print(pattern[index] * 4)
Use np.tile
, you can even avoid using list comprehension:使用np.tile
,您甚至可以避免使用列表理解:
>>> unit = np.repeat([0] * 5 + [1] * 3, 4)
>>> # you can also use unit = [0] * 20 + [1] * 12,
>>> # but using ndarray as the parameter of np.tile is faster than list.
>>> np.tile(unit, 2).reshape(-1, 4)
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
>>> np.tile(unit, 125).reshape(-1, 4)
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.