python中str是什么_numpy中输入的str的默认dtype是什么?

我只是想确认在创建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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值