1

I have a 2D Numpy array of tile objects that serves as a map. The outer ring is all "wall" values to make a closed border. I want to make a copy of the inner values to iterate over without touching the outer rows and columns. I'm trying:

inner_values = map.tiles[1:-1][1:-1]

to cut off the top and bottom rows and left and right columns. My map is 100*70, and this keeps giving me an array of shape (96, 70) when I want (98, 68). How can I use slices correctly to get my inner values? Thanks!

2
  • Tbe second indexing is applied to the result of map.tiles[1:-1]. The net slice is [2:-2]. Not what you want. Commented Jan 4, 2022 at 3:58
  • As a side note, don't use map as variable name. This is a (very useful) python builtin. Commented Jan 4, 2022 at 4:42

1 Answer 1

1

You are just about there...you can put all the indices inside the brackets to get what you want:

import numpy as np

a = np.ones([5, 5])
print(a)

# [[1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]]


a[1:-1, 1:-1] = 0
print(a)
# [[1. 1. 1. 1. 1.]
#  [1. 0. 0. 0. 1.]
#  [1. 0. 0. 0. 1.]
#  [1. 0. 0. 0. 1.]
#  [1. 1. 1. 1. 1.]]

Or given your dimensions:

a = np.ones([100,70])
a[1:-1, 1:-1].shape
# (98, 68)
Sign up to request clarification or add additional context in comments.

Comments

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.