[英]In Python, is there a way to save an index subset of an array to use again later?
My code currently has an array, lets say for example: 我的代码当前有一个数组,例如说:
arr = np.ones((512, 512)).
There is an area of the array that interests me. 我感兴趣的是阵列中的某个区域。 I usually access it like this:
我通常这样访问它:
arr[50:200,150:350] #do stuff here.
I was wondering, is there some way to make a variable that holds [50:200,150:350]
? 我想知道,有什么方法可以使变量保存
[50:200,150:350]
吗? This way, if I need to slightly change my mask, I can do it once, on the top of the file, instead of everywhere it is accessed. 这样,如果我需要略微更改掩码,则可以在文件顶部执行一次操作,而不是在访问它的任何地方进行一次操作。
I tried mask = [50:200,150:350]
, arr[mask]
but Python syntax won't allow that. 我尝试了
mask = [50:200,150:350]
, arr[mask]
但是Python语法不允许这样做。
Thanks for the help! 谢谢您的帮助!
Apparently numpy
extends slicing and allows multiple slice()
objects, one per dimension. 显然
numpy
扩展了切片,并允许多个slice()
对象,每个维度一个。
import numpy
o = numpy.ones((32, 32))
print(o[3:5,3:5])
foo = slice(3,5), slice(3,5)
print(o[foo])
Both incantations produce same result :) 两种咒语产生相同的结果:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.