我目前有以下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_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_1函数中计算此3x3网格内重复项的总数。有什么简单的方法可以将它们组合在一起?
答案 0 :(得分:0)
怎么样:
def nonet_1():
number_of_times_any_value_duplicated = sum([i for i in Counter(m[:3,:3].flatten()).values() if i > 1])
print (number_of_times_any_value_duplicated)