初始化字符串数据的numpy数组的怪异行为 [英] Weird behaviour initializing a numpy array of string data

查看:82
本文介绍了初始化字符串数据的numpy数组的怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数组包含字符串数据时,我在numpy方面遇到了一些琐碎的麻烦.我有以下代码:

I am having some seemingly trivial trouble with numpy when the array contains string data. I have the following code:

my_array = numpy.empty([1, 2], dtype = str)
my_array[0, 0] = "Cat"
my_array[0, 1] = "Apple"

现在,当我用print my_array[0, :]打印它时,得到的响应是['C', 'A'],这显然不是Cat和Apple的预期输出.为什么会这样,如何获得正确的输出?

Now, when I print it with print my_array[0, :], the response I get is ['C', 'A'], which is clearly not the expected output of Cat and Apple. Why is that, and how can I get the right output?

谢谢!

推荐答案

Numpy要求字符串数组具有固定的最大长度.使用dtype=str创建空数组时,默认情况下会将最大长度设置为1.您可以查看是否执行my_array.dtype;它将显示"| S1",表示一个字符的字符串".随后对数组的分配将被截断以适应此结构.

Numpy requires string arrays to have a fixed maximum length. When you create an empty array with dtype=str, it sets this maximum length to 1 by default. You can see if you do my_array.dtype; it will show "|S1", meaning "one-character string". Subsequent assignments into the array are truncated to fit this structure.

您可以通过以下方式传递最大长度的显式数据类型:

You can pass an explicit datatype with your maximum length by doing, e.g.:

my_array = numpy.empty([1, 2], dtype="S10")

"S10"将创建一个长度为10的字符串数组.您必须确定足够大的大小才能容纳要保留的所有数据.

The "S10" will create an array of length-10 strings. You have to decide how big will be big enough to hold all the data you want to hold.

这篇关于初始化字符串数据的numpy数组的怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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