不可变的 numpy 数组? [英] Immutable numpy array?
本文介绍了不可变的 numpy 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种简单的方法可以创建一个不可变的 NumPy 数组?
Is there a simple way to create an immutable NumPy array?
如果必须从 ndarray
派生一个类来实现这一点,那么为了实现不变性而必须重写的最少方法集是什么?
If one has to derive a class from ndarray
to do this, what's the minimum set of methods that one has to override to achieve immutability?
推荐答案
您可以使 numpy 数组不可写:
You can make a numpy array unwriteable:
a = np.arange(10)
a.flags.writeable = False
a[0] = 1
# Gives: ValueError: assignment destination is read-only
另见此线程中的讨论:
http://mail.scipy.org/pipermail/numpy-discussion/2008-December/039274.html
和文档:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html
这篇关于不可变的 numpy 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文