0

How do you add one (+1) to a cell with an existing element in an numpy array? I have a 21x23 zeros array and I want to count occurences by adding one .

for r in holdscore:
    results = np.zeros(shape=(21, 23))
    if one_game(r) < 21:
        results[r,one_game(r)] += 1
    if one_game(r) > 21:
        results[r, 22] += 1    
1
  • 2
    Don't really do numpy, but what you seem to be doing is that you are resetting results for every iteration. Try putting it outside the loop. Commented Apr 12, 2014 at 14:25

1 Answer 1

2

You're incrementing correctly. The problem is that you forget about the old array and make a new one every time through the loop.

Move this statement:

results = np.zeros(shape=(21, 23))

outside the loop:

results = np.zeros(shape=(21, 23))
for r in holdscore:
    if one_game(r) < 21:
        results[r,one_game(r)] += 1
    if one_game(r) > 21:
        results[r, 22] += 1

so it doesn't happen on every iteration.

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

1 Comment

Damn, I was going to put this as an answer, but I wasn't sure. Glad someone else was though, +1.

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.