python的任意长度字符串的numpy数组

时间:2022-10-16 21:26:27

I'm a complete rookie to Python, but it seems like a given string is able to be (effectively) arbitrary length. I.e. you can take a string str and keeping adding to it: str += "some stuff...". Is there a way to make an array of such strings?

我是Python的新手,但似乎给定的字符串可以(有效地)任意长度。也就是说,你可以取一个字符串str加上它:str += "some stuff…"。是否有一种方法来创建这样的字符串数组?

When I try this, each element only stores a single character

当我尝试这个时,每个元素只存储一个字符。

strArr = numpy.empty(10, dtype='string')
for i in range(0,10)
    strArr[i] = "test"

On the other hand, I know I can initialize an array of certain length strings, i.e.

另一方面,我知道我可以初始化某个长度字符串的数组,例如。

strArr = numpy.empty(10, dtype='s256')

which can store 10 strings of up to 256 characters

哪一种可以存储最多256个字符的10个字符串

2 个解决方案

#1


71  

You can do so by creating an array of dtype=object. If you try to assign a long string to a normal numpy array, it truncates the string:

您可以通过创建dtype=object的数组来实现这一点。如果您试图将一个长字符串分配给一个普通的numpy数组,它将截断该字符串:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'])
>>> a[2] = 'bananas'
>>> a
array(['apples', 'foobar', 'banana'], 
      dtype='|S6')

But when you use dtype=object, you get an array of python object references. So you can have all the behaviors of python strings:

但是当您使用dtype=object时,您会得到一个python对象引用数组。所以你可以有python字符串的所有行为:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'], dtype=object)
>>> a
array([apples, foobar, cowboy], dtype=object)
>>> a[2] = 'bananas'
>>> a
array([apples, foobar, bananas], dtype=object)

Indeed, because it's an array of objects, you can assign any kind of python object to the array:

实际上,因为它是一个对象数组,所以您可以为数组分配任何类型的python对象:

>>> a[2] = {1:2, 3:4}
>>> a
array([apples, foobar, {1: 2, 3: 4}], dtype=object)

However, this undoes a lot of the benefits of using numpy, which is so fast because it works on large contiguous blocks of raw memory. Working with python objects adds a lot of overhead. A simple example:

但是,使用numpy的好处很多,因为它在大量连续的原始内存块上工作,所以速度很快。使用python对象会增加很多开销。一个简单的例子:

>>> a = numpy.array(['abba' for _ in range(10000)])
>>> b = numpy.array(['abba' for _ in range(10000)], dtype=object)
>>> %timeit a.copy()
100000 loops, best of 3: 2.51 us per loop
>>> %timeit b.copy()
10000 loops, best of 3: 48.4 us per loop

#2


11  

You could use the object data type:

您可以使用对象数据类型:

>>> import numpy
>>> s = numpy.array(['a', 'b', 'dude'], dtype='object')
>>> s[0] += 'bcdef'
>>> s
array([abcdef, b, dude], dtype=object)

#1


71  

You can do so by creating an array of dtype=object. If you try to assign a long string to a normal numpy array, it truncates the string:

您可以通过创建dtype=object的数组来实现这一点。如果您试图将一个长字符串分配给一个普通的numpy数组,它将截断该字符串:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'])
>>> a[2] = 'bananas'
>>> a
array(['apples', 'foobar', 'banana'], 
      dtype='|S6')

But when you use dtype=object, you get an array of python object references. So you can have all the behaviors of python strings:

但是当您使用dtype=object时,您会得到一个python对象引用数组。所以你可以有python字符串的所有行为:

>>> a = numpy.array(['apples', 'foobar', 'cowboy'], dtype=object)
>>> a
array([apples, foobar, cowboy], dtype=object)
>>> a[2] = 'bananas'
>>> a
array([apples, foobar, bananas], dtype=object)

Indeed, because it's an array of objects, you can assign any kind of python object to the array:

实际上,因为它是一个对象数组,所以您可以为数组分配任何类型的python对象:

>>> a[2] = {1:2, 3:4}
>>> a
array([apples, foobar, {1: 2, 3: 4}], dtype=object)

However, this undoes a lot of the benefits of using numpy, which is so fast because it works on large contiguous blocks of raw memory. Working with python objects adds a lot of overhead. A simple example:

但是,使用numpy的好处很多,因为它在大量连续的原始内存块上工作,所以速度很快。使用python对象会增加很多开销。一个简单的例子:

>>> a = numpy.array(['abba' for _ in range(10000)])
>>> b = numpy.array(['abba' for _ in range(10000)], dtype=object)
>>> %timeit a.copy()
100000 loops, best of 3: 2.51 us per loop
>>> %timeit b.copy()
10000 loops, best of 3: 48.4 us per loop

#2


11  

You could use the object data type:

您可以使用对象数据类型:

>>> import numpy
>>> s = numpy.array(['a', 'b', 'dude'], dtype='object')
>>> s[0] += 'bcdef'
>>> s
array([abcdef, b, dude], dtype=object)