Python数据类型:'| S ......'是什么意思?

时间:2016-01-20 20:05:11

标签: python numpy

一个菜鸟问题,但在这个numpy数组中:

array(['22', '38', '26', '35', '35', '', '54', '2', '27', '14', '4', '58',    '20', '39', '14'], dtype='|S82')

'| S82'是什么意思?

3 个答案:

答案 0 :(得分:3)

Per the docs它定义了数据类型的字节顺序,种类和大小。在这种情况下:

byte-order: |   not applicable
kind:       S   (byte-)string
size:       82  (characters long)

答案 1 :(得分:0)

尝试:

In [292]: np.array(['one', 'two','three']) # a
Out[292]: array(['one', 'two', 'three'], dtype='<S5')

效率,numpy对齐数据。在这里,所有项目都是最多5个字符的字符串,用于托管初始'three'

In [295]: a[1]='seventeen';a
Out[295]: array(['one', 'seven', 'three'], dtype='<S5')

你不能再使用更长的字符串了。

答案 2 :(得分:0)

NumPy documentation开始,dtype的第一个字母会告诉您它是什么类型的数据。

Array-protocol type strings (see The Array Interface) 

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  
  'b'       boolean  
  'i'       (signed) integer   
  'u'       unsigned integer  
  'f'       floating-point  
  'c'       complex-floating point  
  'O'       (Python) objects  
  'S', 'a'  (byte-)string  
  'U'       Unicode  
  'V'       raw data (void)  

因此,在您的示例中,它指定的是一个长度为82个字符的字节字符串。

相关问题