函数中的Python计数器函数

2024-04-25 01:40:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前拥有以下9x9阵列:

m = np.zeros((9,9)) #9x9 grid of all zeros 
vals = np.arange(1,10) #set of values from 1 to 9 aranged randomly
for i in range(0,9):
    m[i,:] = np.random.choice(vals,size=9,replace=False) #randomly choses a set of 9 values for the row
print(m.astype(int)) #prints as integers

我用它创建了一个名为nonet\u 1的函数:

def nonet_1():
    for i in range(0,3):
        for j in range(0,3):
            print(m[i,j])
nonet_1()

我想使用一个类似的想法,如下,使用计数器函数。你知道吗

[r - 1 for r in Counter((m[:,i])).values()] #this line of code produces the scores based on n.o repeats in each column

总体目标是在nonet\ U 1函数中计算这个3x3网格中的重复数。有没有一个简单的方法我可以把这些结合起来?你知道吗


Tags: ofthe函数infornpzerosrange