问题

I was experimenting with numpy arrays and created a numpy array of strings:

ar1 = np.array(['avinash', 'jay'])

As I have read from from their official guide, operations on numpy array are propagated to individual elements. So I did this:

ar1 * 2

But then I get this error:

TypeError Traceback (most recent call last)

in ()

----> 1 ar1 * 2

TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'int'

But when I used dtype=object

ar1 = np.array(['avinash', 'jay'], dtype=object)

while creating the array I am able to do all operations.

Can anyone tell me why this is happening?

解决方法

NumPy arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memory are interpreted as values with that datatype.

Creating an array with dtype=object is different. The memory taken by the array now is filled with pointers to Python objects which are being stored elsewhere in memory (much like a Python list is really just a list of pointers to objects, not the objects themselves).

Arithmetic operators such as * don't work with arrays such as ar1 which have a string_ datatype (there are special functions instead - see below). NumPy is just treating the bits in memory as characters and the * operator doesn't make sense here. However, the line

np.array(['avinash','jay'], dtype=object) * 2

works because now the array is an array of (pointers to) Python strings. The * operator is well defined for these Python string objects. New Python strings are created in memory and a new object array with references to the new strings is returned.

If you have an array with string_ or unicode_ dtype and want to repeat each string, you can use np.char.multiply:

In [52]: np.char.multiply(ar1, 2)

Out[52]: array(['avinashavinash', 'jayjay'],

dtype='

python中dtype是object是什么意思_dtype = object在创建numpy数组时意味着什么? - python代码 - 源码查...相关推荐

  1. python中if语句求最大值_如何用if语句向量化numpy数组中的最大值?

    我的设置:Python 2.7.4.1.numpymkl 1.7.1.windows7x64.WinPython 上下文: 我尝试实现序列最小优化算法来求解支持向量机.我使用最大违反对方法.在 问题是 ...

  2. html文件用safari打开方式,如何使用openURL()读取在safari中打开的html文件 - c#代码 - 源码查...

    问题 I am trying to open a page using UIApplication.sharedApplication.openURL(url) I do few clicks and ...

  3. java settitle 源码_在其他初始化方法中折叠工具栏的setTitle - java代码 - 源码查

    问题 In my Fragment I have a CollapsingToolbar and it is created it in the onCreateView() method: @Ove ...

  4. python中append函数合并列表且列表内数字从高到低_35个高级Python知识点总结

    No.1 一切皆对象 众所周知,Java中强调"一切皆对象",但是Python中的面向对象比Java更加彻底,因为Python中的类(class)也是对象,函数(function) ...

  5. python中使用zip函数基于两个列表数据list创建字典dict数据(Create a dictionary by passing the output of zip to dict)

    python中使用zip函数基于两个列表数据list创建字典dict数据(Create a dictionary by passing the output of zip to dict) 目录

  6. Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)

    Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名) 目录

  7. Python中使用逻辑与运算符“and”判断两个比较条件是否满足时编辑器提示“Simplify chained comparison ”的解决方法

    Python中使用逻辑与运算符"and"判断两个比较条件是否满足时编辑器提示"Simplify chained comparison ", 如下图所示: 这是提 ...

  8. 雷达信号处理程序源码 python 需达围像识别 雷达系统仿真代码源码 matlab SAR 雷达系统仿真程序集合 雷达压制干扰matlab程序 线性调联(IFM)脉冲压缩雷达伤真

    雷达信号处理程序源码 雷达系统仿真代码源码 matlab SAR 1.MAITOFPR 探地雷达数据处理MMITLAB程序 2.python 需达围像识别 3.SAR团达回波仿真matlab 4.SA ...

  9. 第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图...

    第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲-爬虫和反爬的对抗过程以及策略-scrapy架构源码分析图 1.基本概念 2.反爬虫的目的 3.爬虫和反爬的对抗过程以及策略 scra ...

  10. Python量化代码源码160个,聚宽直接使用,已全部整理

    Python量化代码源码160个,聚宽直接使用,已全部整理.包含截面策略,择时策略,神经网络,机器学习,随机森林 id=659330254988&

最新文章

  1. 《构建高性能web站点》随笔 无处不在的性能问题
  2. SylixOS普通定时器精度分析
  3. 局域网屏幕共享_ShareMouse for Mac(鼠标键盘共享)
  4. 【经验】刚读硕士怎么感觉学机器学习和深度学习越学越不懂?
  5. OBD技术速成——J1850协议解析软件实现
  6. java中包容易出现的错误及权限问题
  7. C语言orC++,最大的区别?
  8. QT_Qsplitter
  9. 通过JMETER后置处理器JSON Path Extractor插件来获取响应结果
  10. IOCAutofac与ORMEntityFramwork的联系--单例模式
  11. matlab贝塞尔函数零点,第一类贝塞尔函数零点求解matlab程序
  12. java读取propertiies文件例子
  13. C语言实现峰度的计算
  14. 旅美见闻:美国贫民百姓众生相
  15. 【PAT】B1032 挖掘机技术哪家强 (20 分)_C语言实现
  16. 微信小程序本地图片安卓手机不显示而苹果手机显示问题
  17. 【React学习】React中ref的用法
  18. python词云生成的图片为空白图片
  19. 51单片机 Proteus仿真 时钟 串口 发送时钟 整点报时
  20. 分享150个ASP源码,总有一款适合您

热门文章

  1. 【FlinkX】两个issue分析:reader和writer的通道数不一致+获取JobId
  2. vscode之 wget下载zip文件并unzip解压zip文件
  3. t999主页劫持/篡改木马
  4. VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面,直接使用自带远程工具)...
  5. Xmind思维导图 常用快捷键使用
  6. Scale和Resolution的含义及转换算法
  7. esxi7.0 打包网卡驱动
  8. 制作一键架设服务器,制作一键端方法
  9. 服务器装系统报0x0000005d,安装Win8系统提示Error Code:0x0000005D错误怎么办
  10. win10没法进入安全模式的处理办法