numpy 中的逐元素字符串连接 [英] Element-wise string concatenation in numpy

查看:21
本文介绍了numpy 中的逐元素字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个错误吗?

将 numpy 导入为 npa1=np.array(['a','b'])a2=np.array(['E','F'])在 [20] 中:添加(a1,a2)出[20]:未实施

我正在尝试按元素进行字符串连接.我认为 Add() 是在 numpy 中做到这一点的方法,但显然它没有按预期工作.

解决方案

这可以使用 numpy.core.defchararray.add.下面是一个例子:

<预><代码>>>>将 numpy 导入为 np>>>a1 = np.array(['a', 'b'])>>>a2 = np.array(['E', 'F'])>>>np.core.defchararray.add(a1, a2)数组(['aE','bF'],dtype='<U2')

还有其他有用的字符串操作可用于 NumPy 数据类型.

Is this a bug?

import numpy as np
a1=np.array(['a','b'])
a2=np.array(['E','F'])

In [20]: add(a1,a2)
Out[20]: NotImplemented

I am trying to do element-wise string concatenation. I thought Add() was the way to do it in numpy but obviously it is not working as expected.

解决方案

This can be done using numpy.core.defchararray.add. Here is an example:

>>> import numpy as np
>>> a1 = np.array(['a', 'b'])
>>> a2 = np.array(['E', 'F'])
>>> np.core.defchararray.add(a1, a2)
array(['aE', 'bF'], 
      dtype='<U2')

There are other useful string operations available for NumPy data types.

这篇关于numpy 中的逐元素字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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