将分隔符分隔的字符串转换为 numpy 数组的有效方法 [英] Efficient way to convert delimiter separated string to numpy array

查看:27
本文介绍了将分隔符分隔的字符串转换为 numpy 数组的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串如下:

1|234|4456|789

我必须把它转换成numpy数组.我想知道最有效的方法.因为我将调用这个函数超过5000万次!

解决方案

最快的方法是使用 numpy.fromstring 方法:

<预><代码>>>>导入 numpy>>>数据 = "1|234|4456|789">>>numpy.fromstring(data, dtype=int, sep="|")数组([ 1, 234, 4456, 789])

I have a String as follows :

1|234|4456|789

I have to convert it into numpy array.I would like to know the most efficient way.Since I will be calling this function for more than 50 million times!

解决方案

The fastest way is to use the numpy.fromstring method:

>>> import numpy
>>> data = "1|234|4456|789"
>>> numpy.fromstring(data, dtype=int, sep="|")
array([   1,  234, 4456,  789])

这篇关于将分隔符分隔的字符串转换为 numpy 数组的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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