[英]What is the difference between the types <type 'numpy.string_'> and <type 'str'>?
类型<type 'numpy.string_'>
和<type 'str'>
<type 'numpy.string_'>
之间是否存在差异?
numpy.string_
is the NumPy datatype used for arrays containing fixed-width byte strings. numpy.string_
是用于包含固定宽度字节字符串的数组的NumPy数据类型。 On the other hand, str
is a native Python type and can not be used as a datatype for NumPy arrays*. 另一方面, str
是本机Python类型,不能用作NumPy数组*的数据类型。
If you create a NumPy array containing strings, the array will use the numpy.string_
type (or the numpy.unicode_
type in Python 3). 如果创建包含字符串的NumPy数组,则该数组将使用numpy.string_
类型(或Python 3中的numpy.unicode_
类型)。 More precisely, the array will use a sub-datatype of np.string_
: 更准确地说,该数组将使用np.string_
的子数据类型:
>>> a = np.array(['abc', 'xy'])
>>> a
array(['abc', 'xy'], dtype='<S3')
>>> np.issubdtype('<S3', np.string_)
True
In this case the datatype is '<S3'
: the <
denotes the byte-order (little-endian), S
denotes the string type and 3
indicates that each value in the array holds up to three characters (or bytes). 在这种情况下,数据类型为'<S3'
: <
表示字节顺序(little-endian), S
表示字符串类型, 3
表示数组中的每个值最多包含三个字符(或字节)。
One property that np.string_
and str
share is immutability. np.string_
和str
共享的一个属性是不可变性。 Trying to increase the length of a Python str
object will create a new object in memory. 尝试增加Python str
对象的长度将在内存中创建一个新对象。 Similarly, if you want fixed-width NumPy array to hold more characters, a new larger array will have to be created in memory. 同样,如果您希望固定宽度的NumPy数组包含更多字符,则必须在内存中创建一个新的更大的数组。
* Note that it is possible to create a NumPy object
array which contains references to Python str
objects, but such arrays behave quite differently to normal arrays. *请注意,可以创建一个NumPy object
数组,其中包含对Python str
对象的引用 ,但此类数组的行为与普通数组的行为完全不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.