numpy.chararray发生了什么 [英] What happened to numpy.chararray

查看:96
本文介绍了numpy.chararray发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#Input: 
chararr = np.chararray((3, 5))
chararr[:] = 'a'
chararr

#Output: 
chararray([[b'a', b'a', b'a', b'a', b'a'],
   [b'a', b'a', b'a', b'a', b'a'],
   [b'a', b'a', b'a', b'a', b'a']], 
  dtype='|S1')

我的问题是'b'是从哪里来的...我是从jupyter笔记本电脑和PyCharm那里得到的

My question is where does that 'b' come from ... I got this from jupyter notebook and PyCharm

推荐答案

在Python3中,默认字符串类型为unicode.字节串与b标志一起显示.注意<S1 dtype吗?这意味着字节,<U1用于unicode(Py2和Py3均适用).

In Python3, the default string type is unicode. Bytestrings are displayed with the b flag. Notice the <S1 dtype? That means bytes, <U1 is for unicode (that's true for both Py2 and Py3).

chararray具有一个unicode参数.

In [161]: A=np.chararray((3,5),unicode=True)
In [162]: A[:]='a'
In [163]: A
Out[163]: 
chararray([['a', 'a', 'a', 'a', 'a'],
       ['a', 'a', 'a', 'a', 'a'],
       ['a', 'a', 'a', 'a', 'a']], 
      dtype='<U1')

如果我在Py2中也做过同样的事情,那我会看到u'a'.

If I did the same in Py2, I'd be seeing u'a'.

这篇关于numpy.chararray发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆