1

我有以下矩阵:

W = [['a', 'b', 'b', 'b', 'a'],
     ['a', 'a', 'a', 'a', 'b'],
     ['a', 'b', 'a', 'b', 'b'],
     ['a', 'a', 'a', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b']]

如果我从矩阵中选择 W[x][y],我如何计算该特定点周围的 a 字符?例如,假设我选择 W[4][1],即 b。我可以看到那个点周围有三个a。但是我如何通过编码来确定它呢?我最终得到了非常混乱的代码,并意识到如果我们改变矩阵的维度,它就不会起作用。问题还在于,如果我选择靠近回合的点,W[y-1][(x):(x+1)].count("a")那么这种想法就行不通了。

再次,我们将不胜感激!谢谢!

更新:

W     = [['K', ' ', ' ', ' ', ' '],
         ['K', 'K', 'K', 'K', ' '],
         ['K', ' ', 'K', ' ', ' '],
         ['K', 'K', 'K', ' ', ' '],
         [' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ']]

def count_matrix(W, y, x, ch="K"):
    ref = (-1, 0, 1)
    occ = []
    a = "K"
    b = " "
    for dy, dx in [(a, b) for a in ref for b in ref if (a,b)!=(0, 0)]:
        if (x+dx) >= 0 and (y+dy) >= 0:
            try:
                occ.append(W[x+dx][y+dy])
            except IndexError:
                pass
    return occ.count(ch)
print count_matrix(W,0,0)

这返回0

4

3 回答 3

2

这有效:

W = [['a', 'b', 'b', 'b', 'a'],
     ['a', 'a', 'a', 'a', 'b'],
     ['a', 'b', 'a', 'b', 'b'],
     ['a', 'a', 'a', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b']]

def count_matrix(W, r,c, ch):
    ref=(-1,0,1)
    matches=[]
    for dr, dc in [(a, b) for a in ref for b in ref if (a,b)!=(0,0)]:
        if r+dr>=0 and c+dc>=0:
            try:
                matches.append(W[r+dr][c+dc])
            except IndexError:
                pass
    return matches.count(ch)

print count_matrix(W,0,0,'a')   # correctly handles upper LH 
# 2
print count_matrix(W,4,1,'a') 
# 3

if (a,b)!=(0,0)如果要计算正方形本身中的字符,请删除。即,在正方形0,0中,您是否计算那里的“a”?

于 2013-11-02T00:54:41.273 回答
1

解释见代码注释:

from itertools import product

W = [['a', 'b', 'b', 'b', 'a'],
     ['a', 'a', 'a', 'a', 'b'],
     ['a', 'b', 'a', 'b', 'b'],
     ['a', 'a', 'a', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b']]

def count_chars(W, x, y, ch):
    return sum(W[x+dx][y+dy] == ch
                   for dx, dy in product([-1,0,1], [-1,0,1])
                       if 0 <= x+dx < len(W) and 0 <= y+dy < len(W[0]))
于 2013-11-01T22:58:43.887 回答
0

Regarding to problem with the original matrix:

W = [['a', 'b', 'b', 'b', 'a'],
     ['a', 'a', 'a', 'a', 'b'],
     ['a', 'b', 'a', 'b', 'b'],
     ['a', 'a', 'a', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b']]

How about this?

def count_matrix(W, x, y):
    occ = 0
    for n in range(max(0, y - 1), min(len(W), y + 2)):
        for m in range(max(0, x - 1), min(len(W[0]), x + 2)):
            if W[n][m] == "a":
                occ += 1
    return occ

print count_matrix(W, 0, 0)

retunrs 3. Only problem left is how to exclude the value of square itself?

EDIT:

def count_matrix(W, x, y):
occ = 0
for n in range(max(0, y - 1), min(len(W), y + 2)):
    for m in range(max(0, x - 1), min(len(W[0]), x + 2)): 
        if n == y and m == x:
            pass
        else: 
            if W[n][m] == "a":
                occ += 1
return occ

W = [['a', 'b', 'b', 'b', 'a'],
     ['a', 'a', 'a', 'a', 'b'],
     ['a', 'b', 'a', 'b', 'b'],
     ['a', 'a', 'a', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b'],
     ['b', 'b', 'b', 'b', 'b']]

print count_matrix(W, 1, 4)

Returns 3 as expected.

print count_matrix(W, 0, 0)

Returns 2 as expected. Is this good practise?

于 2013-11-02T08:34:06.420 回答