1

I have following 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']]

If I choose W[x][y] from matrix, how can I count a-characters around that spesific point? For example let's say I choose W[4][1] which is b. I can see there is three a's around that point. But how can I determine it by coding? I ended up really messy peace of code, and realized it would not have worked if we change the dimension of matrix. Problem was also that if I choose the point near rounds, W[y-1][(x):(x+1)].count("a") -kind of thinking doesn't work.

Again, help would be appreciated! Thanks!

UPDATE:

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)

This returns 0.

2
  • 1
    typo: ref = (-1, 0, -1) Commented Nov 2, 2013 at 8:30
  • Your assignment of a='K' and b=" " is not doing what you think. The a and b inside the list comprehension are just temp variables with namespace scope limited to the comprehension. Setting a and b to other values outside the comprehension does not change anything. Commented Nov 2, 2013 at 16:30

3 Answers 3

2

This works:

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

Remove the if (a,b)!=(0,0) if you want to count the character in the square itself. ie, With the square 0,0 do you count the 'a' there or not?

Sign up to request clarification or add additional context in comments.

1 Comment

No I do not count it. But I ended up in troubles again, if I let user to determine a and b themselves. Let's say user chooses a = "K" and b = " ". Still I would like to be able to count "K":s in the matrix without errors. Any suggestions?
1

See code comments for explanation:

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

3 Comments

Ok, so it's just about ignoring IndexError's it is causing.. Funny though, I thought this could be done more easily. But will try this, thanks!
This is incorrect in several ways: 1) any top row or left col will switch to relative indexing since W[0-1] or W[][0-1] will now give the end of that list 2) Because of '1' the try is never triggered; there is never an index error 3) it counts the square with no offset W[x][y] since product([-1,0,1], [-1,0,1] will produce the tuple (0,0) as one of the results.
1. agree, removed wrong answer since it's modification to being correct make it also a bit messy. 2. index error is not triggerd only for 0's row, but it is not a point in view of (1) 3. see sample code where (0,0) is counted as well
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?

1 Comment

This doesn't cause any errors even if matrix is 1x1 matrix. So this just the code I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.