简体   繁体   English

如何在numpy中为字符串赋值?

[英]How to assign a string value to an array in numpy?

When I try to assign a string to an array like this: 当我尝试将字符串分配给这样的数组时:

CoverageACol[0,0] = "Hello" 

I get the following error 我收到以下错误

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    CoverageACol[0,0] = "hello"
ValueError: setting an array element with a sequence.

However, assigning an integer does not result in an error: 但是,分配整数不会导致错误:

CoverageACol[0,0] = 42

CoverageACol is a numpy array. CoverageACol是一个numpy数组。

Please help! 请帮忙! Thanks! 谢谢!

You get the error because NumPy's array is homogeneous, meaning it is a multidimensional table of elements all of the same type . 你得到错误是因为NumPy的数组是同构的,这意味着它是一个多维表格的所有相同类型的元素 This is different from a multidimensional list-of-lists in "regular" Python, where you can have objects of different type in a list. 这与“常规”Python中的多维列表列表不同,您可以在列表中包含不同类型的对象。

Regular Python: 常规Python:

>>> CoverageACol = [[0, 1, 2, 3, 4],
                    [5, 6, 7, 8, 9]]

>>> CoverageACol[0][0] = "hello"

>>> CoverageACol
    [['hello', 1, 2, 3, 4], 
     [5, 6, 7, 8, 9]]

NumPy: NumPy的:

>>> from numpy import *

>>> CoverageACol = arange(10).reshape(2,5)

>>> CoverageACol
    array([[0, 1, 2, 3, 4],
           [5, 6, 7, 8, 9]])

>>> CoverageACol[0,0] = "Hello" 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/biogeek/<ipython console> in <module>()

ValueError: setting an array element with a sequence.

So, it depends on what you want to achieve, why do you want to store a string in an array filled for the rest with numbers? 那么,这取决于你想要实现的目标,为什么你要将一个字符串存储在一个数组中? If that really is what you want, you can set the datatype of the NumPy array to string: 如果这真的是你想要的,你可以将NumPy数组的数据类型设置为string:

>>> CoverageACol = array(range(10), dtype=str).reshape(2,5)

>>> CoverageACol
    array([['0', '1', '2', '3', '4'],
           ['5', '6', '7', '8', '9']], 
           dtype='|S1')

>>> CoverageACol[0,0] = "Hello"

>>> CoverageACol
    array([['H', '1', '2', '3', '4'],
         ['5', '6', '7', '8', '9']], 
         dtype='|S1')

Notice that only the first letter of Hello gets assigned. 请注意,只分配Hello的第一个字母。 If you want the whole word to get assigned, you need to set an array-protocol type string : 如果要分配整个单词,则需要设置数组协议类型字符串

>>> CoverageACol = array(range(10), dtype='a5').reshape(2,5)

>>> CoverageACol: 
    array([['0', '1', '2', '3', '4'],
           ['5', '6', '7', '8', '9']], 
           dtype='|S5')

>>> CoverageACol[0,0] = "Hello"

>>> CoverageACol
    array([['Hello', '1', '2', '3', '4'],
           ['5', '6', '7', '8', '9']], 
           dtype='|S5')

You need to set the data type of the array : 您需要设置数组数据类型

CoverageACol = numpy.array([["a","b"],["c","d"]],dtype=numpy.dtype('a16'))

This makes ConerageACol an array of strings (a) of length 16. 这使得ConerageACol成为长度为16的字符串(a)数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM