我只是想确认在创建ndarray时,字符串的默认数据类型是否为unicode.我找不到任何可以清楚说明这一点的参考.可能是太明显了,不需要说明.
当指定dtype时:
>>> import numpy as np
>>> g = np.array([['a', 'b'],['c', 'd']], dtype='S')
>>> g
array([[b'a', b'b'],
[b'c', b'd']],
dtype='|S1')
不指定dtype:
>>> g = np.array([['a', 'b'],['c', 'd']])
>>> g
array([['a', 'b'],
['c', 'd']],
dtype='
同样,当指定dtype时,文字b表示什么.根据文档,它表明“笨蛋”在这里似乎并非如此.
可以请一个人澄清一下吗?
解决方法:
b’…’表示它是一个字节字符串,字符串数组的默认dtype取决于字符串的类型. Unicode(Python 3字符串是unicode)是U,Python 2 str或Python 3个字节具有dtypeS.您可以在NumPy documentation here中找到dtypes的说明.
Array-protocol type strings
The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised. The supported kinds are:
‘?’ boolean
‘b’ (signed) byte
‘B’ unsigned byte
‘i’ (signed) integer
‘u’ unsigned integer
‘f’ floating-point
‘c’ complex-floating point
‘m’ timedelta
‘M’ datetime
‘O’ (Python) objects
‘S’, ‘a’ zero-terminated bytes (not recommended)
‘U’ Unicode string
‘V’ raw data (void)
但是,在第一种情况下,实际上您是因为指定了dtype =’S’而实际上强迫NumPy将其转换为字节.
标签:numpy-dtype,string,python,numpy
来源: https://codeday.me/bug/20191025/1930782.html