如何在Python中有效地重复矩阵中的二进制模式和比率?

发布于 2025-01-23 18:50:48 字数 194 浏览 1 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

您可以使用np.block的组合和列表理解:

out = np.block([[np.zeros((5, 4))],[np.ones((3, 4))]] * 125).astype(int)

可以将其功能化以回答类似的问题类似的问题:

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.]])

You can use a combination of np.block and list comprehension:

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.]])
生来就爱笑 2025-01-30 18:50:48

您可以做到这一点:

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)])

您可以检查形状。它有125行8行IE 1000:

>>> my_array.shape
(125, 8, 4)

要打印它可以使用:

count_row = 0
for row in my_array:
    for row2 in row:
        print(row2)
        count_row += 1

输出:

# 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]
....

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 i.e. 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:

# 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]
....
童话 2025-01-30 18:50:48

这没有使用numpy,但它可以完成工作。您以后可以使用numpy.matrix将其转换为numpy矩阵

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)

输出 -

00000
00000
00000
00000
00000
11111
11111
11111
00000
00000
00000
00000
00000
11111
11111
11111

This didn't use numpy but it does the job. You can later convert it into a numpy matrix using 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 -

00000
00000
00000
00000
00000
11111
11111
11111
00000
00000
00000
00000
00000
11111
11111
11111
辞旧 2025-01-30 18:50:48

本着@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)

In the spirit of @Ishan golfing this, here it is in one line, no librarys:

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)
小梨窩很甜 2025-01-30 18:50:48

使用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]])

Use np.tile, you can even avoid using list comprehension:

>>> 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]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文