python中字典的优化和打印值
我有一个NxM矩阵,在矩阵的随机索引中以0和1编码(1表示要绘制的像素,0表示空白)。我想把字典中的所有元素分组,并将它们作为一个命令打印出来,而不是为了优化而单独迭代,然后将其放回原始矩阵中以创建其最终形状(俄罗斯方块游戏) 矩阵如下所示:python中字典的优化和打印值,python,dictionary,matrix,optimization,Python,Dictionary,Matrix,Optimization,我有一个NxM矩阵,在矩阵的随机索引中以0和1编码(1表示要绘制的像素,0表示空白)。我想把字典中的所有元素分组,并将它们作为一个命令打印出来,而不是为了优化而单独迭代,然后将其放回原始矩阵中以创建其最终形状(俄罗斯方块游戏) 矩阵如下所示: [[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
[0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1]
[1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1]
[1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1]
[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]
我的代码如下:
def solution(imat, painter):
nrows, ncols = imat.shape # gets the shape of the matrix (20 rows and 20 columns)
start = 0
end = 0
rows = dict.fromkeys(range(nrows), []) #creates the dictionary
for r in range(nrows): # loops through the number of rows
for c in range(ncols): # loops through the number of columns
if imat[r,c] == 1: # checks if at a specific index there exists a 1
rows[r].append((r,c)) # if so it appends it to the dictionary
painter.paint('square', r, c, 1) # prints the 1s, but as individuals instead of a block.
我可以将所有的1附加到字典中,但无法将所有的1作为一个对象并放回原始矩阵(即imat.shape
)
最终的输出应该是这样的,命令越少越好
有关更多信息,请参见:
非常感谢所有的帮助和建议我为一个简单的压缩算法做了一个示例。其思想是计算一行中有多少个
0
s和1
s。在最坏的情况下,您根本不会得到压缩,但您的数据似乎是相当分组的,所以一般来说,您应该会看到相当多的压缩
我还添加了一个关于如何打印这些数据的示例,因为它最终似乎是您想要的。将print_block
方法更改为使用任何用于绘制屏幕的东西——我在这里向您展示如何使用它“分块”打印压缩数据
m = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 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 0 0 0 1 0 1 1
Result
0 2 2 3 1
4 1 1 2
"""
nm = []
for r in mat:
nr = []
gather = 0
acc = 0
for c in r:
if c == gather:
acc += 1
else:
nr.append(acc)
gather = int(not gather)
acc = 1
nr.append(acc)
nm.append(nr)
return nm
print_calls = 0
def print_block(value, nbr):
global print_calls
print_calls += 1
for i in range(nbr):
print(value, end=" ")
def print_compressed(mat):
for r_block in mat:
for i, nbr in enumerate(r_block):
print_block(i % 2, nbr) # change this line to draw your actual board
print()
cm = compress(m)
print_compressed(cm)
print('print_calls =', print_calls)
这是给我的
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
print_calls = 70
打印调用的减少从20*20=400->70。也就是说,减少了82.5%。我做了一个简单压缩算法的示例。其思想是计算一行中有多少个
0
s和1
s。在最坏的情况下,您根本不会得到压缩,但您的数据似乎是相当分组的,所以一般来说,您应该会看到相当多的压缩
我还添加了一个关于如何打印这些数据的示例,因为它最终似乎是您想要的。将print_block
方法更改为使用任何用于绘制屏幕的东西——我在这里向您展示如何使用它“分块”打印压缩数据
m = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
def compress(mat):
""" Compress each row by counting how many 0's and 1's are in a row.
Example:
1 1 0 0 1 1 1 0
0 0 0 0 1 0 1 1
Result
0 2 2 3 1
4 1 1 2
"""
nm = []
for r in mat:
nr = []
gather = 0
acc = 0
for c in r:
if c == gather:
acc += 1
else:
nr.append(acc)
gather = int(not gather)
acc = 1
nr.append(acc)
nm.append(nr)
return nm
print_calls = 0
def print_block(value, nbr):
global print_calls
print_calls += 1
for i in range(nbr):
print(value, end=" ")
def print_compressed(mat):
for r_block in mat:
for i, nbr in enumerate(r_block):
print_block(i % 2, nbr) # change this line to draw your actual board
print()
cm = compress(m)
print_compressed(cm)
print('print_calls =', print_calls)
这是给我的
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
print_calls = 70
打印调用的减少从20*20=400->70。也就是说,减少了82.5%。“我想将字典中的所有内容分组,并将它们作为一个命令打印出来,而不是为了优化目的而单独迭代。”不太清楚这是一种优化。要构建字典,你需要查看所有元素,如果我没记错的话,那就是N^2,在字典中添加一个项目是NlogNI,我也不太理解这一点。为什么字典更好?如果您担心内存占用,那么数据似乎很容易压缩。目标是基本上用尽可能少的指令打印矩阵-而不是通过每次迭代(每次一个命令)搜索来打印它,我希望将其打印成块-我不太确定如何做。“我希望将字典中的所有命令分组,并将它们作为一个命令打印出来,而不是为了优化而单独迭代“不太确定这是一种优化。要构建字典,你需要查看所有元素,如果我没记错的话,那就是N^2,在字典中添加一个项目是NlogNI,我也不太理解这一点。为什么字典更好?如果你担心内存占用的话,数据似乎很容易压缩。目标是基本上用尽可能少的指令打印矩阵-而不是通过每次迭代都搜索一个命令来打印,我想成批打印-我不太确定如何做。非常感谢,这真的很有帮助。非常感谢。有没有办法通过排除所有0,只压缩新矩阵中的1?我无法想象这会对你有任何帮助。如果要删除0的draw调用,请不要为这些值调用
print\u block
函数。非常感谢您的帮助,它确实很有帮助。非常感谢。有没有办法通过排除所有0,只压缩新矩阵中的1?我无法想象这会对你有任何帮助。如果要删除0的draw调用,只需不调用那些值的print\u块
函数即可。