Coolcrab
Coolcrab

Reputation: 2723

Python dictionary hold array ranges?

I am tryng to make a dict that could hold some array sniplets like [127:130, 122:124] but dict = {1:[127:130, 122:124], 2:[127:129, 122:123]} doesn't work.

Is there a way to do this? It doesn't need to be dicts, but I want a bunch of these areas to be callable.

So I have 256x256 arrays and I want to select small areas in them for some calculations: fft[127:130, 122:124]

Would be great if the whole part between brackets could be in a dict

Upvotes: 1

Views: 83

Answers (2)

ammonite
ammonite

Reputation: 206

You could use the slice function. It returns a slice object that can be stored in a dictionary. eg:

slice_1 = slice(127, 130)
slice_2 = slice(122, 124)

slice_a = slice(127, 129)
slice_b = slice(122, 123)

d = {1:[slice_1, slice_2],
     2:[slice_a, slice_b]
     }

x = fft[d[1]]  # Same as fft[127:130, 122:124]
y = fft[d[2]]  # Same as fft[127:129, 122:123]

Upvotes: 2

data
data

Reputation: 2813

Slicing numpy arrays returns a view, and not a copy, maybe this is what you are looking for?

import numpy

a = numpy.arange(10)

b = a[3:6] # array([3, 4, 5])
a[4] = 0
#b is now array([ 3, 0,  5])

b[1] = 1

#a is now array([0, 1, 2, 3, 1, 5, 6, 7, 8, 9])

Upvotes: 0

Related Questions