简体   繁体   English

在Python中,有没有一种方法可以保存数组的索引子集以供以后使用?

[英]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.

相关问题 有没有办法将多个图像保存到 python 中的数组以供以后使用? - Is there a way to save multiple images to an array in python for later use? 如何将numpy数组保存到计算机中供以后在python中使用 - How to save numpy array into computer for later use in python 如何在 python 中保存 C++ 指针以便稍后再次传递? - How save a C++ pointer in python to pass it again later? 有没有办法保存验证码图像并稍后在python中查看? - Is there a way to save a captcha image and view it later in python? python 是否在同一行缓存项目并稍后再次使用 - Does python cache items in the same line and use again later 有没有一种简单的方法可以使用行索引将数据子集存储到 DataFrames 中,最终使用 Python Pandas 连接回 CSV - Is there an easy way to use row index to store subset of data into DataFrames to finally concat back into a CSV using Python Pandas 使用enumerate(array)Python访问数组中的更高索引 - Accessing later index in array using enumerate(array) Python 将嵌套列表/数组保存为CSV以便日后轻松加载的最佳方法 - Best way to save nested list/array to CSV for easy loading later 保存python词典并在以后检索它们的正确方法 - Proper way to save python dictionaries and retrieve them at a later stage 有没有办法可以保存用户输入然后在 python 中重用它? - is there a way i can save user input then reuse it later in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM