【问题标题】:In Python, is there a way to save an index subset of an array to use again later?在 Python 中,有没有办法保存数组的索引子集以供以后再次使用?
【发布时间】:2019-06-21 06:29:33
【问题描述】:

我的代码当前有一个数组,例如:

arr = np.ones((512, 512)).

阵列中有一个区域让我感兴趣。我通常这样访问它:

arr[50:200,150:350] #do stuff here.

我想知道,有没有办法制作一个包含[50:200,150:350] 的变量?这样,如果我需要稍微改变我的掩码,我可以在文件顶部做一次,而不是在任何地方访问它。

我尝试了mask = [50:200,150:350]arr[mask],但 Python 语法不允许这样做。

感谢您的帮助!

【问题讨论】:

标签: python arrays


【解决方案1】:

显然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])

两种咒语产生相同的结果:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多