简体   繁体   English

Python-Numpy数组中的字符串和整数

[英]Python - strings and integers in Numpy array

I want to create an array with 3 columns. 我想创建一个包含3列的数组。 The first one a string, the other two integers used for calculations. 第一个是字符串,其他两个整数用于计算。 Then more rows will be added through the append function (below). 然后,更多的行将通过append函数(如下)添加。

The problem is that all columns seem to be coded as strings rather than just the first one. 问题在于所有列似乎都编码为字符串,而不仅仅是第一个。 How do I get the correct data type for the numbers? 如何获得数字的正确数据类型?

a = np.array([["String",1,2]])
a = np.append(a, [["another string", 3, 4]],axis = 0)

To have such a mixed datatype data, we could use object as dtype before appending or stacking - 为了拥有这样一个混合的数据类型数据,我们可以在追加或堆叠之前将object用作dtype-

a = np.array([["String",1,2]], dtype=object)
b = [["another string", 3, 4]]
a = np.vstack((a,np.asarray(b,object)))

Sample run - 样品运行-

In [40]: a = np.array([["String",1,2]], dtype=object)

In [41]: b = [["another string", 3, 4]]

In [42]: np.vstack((a,np.asarray(b,object)))
Out[42]: 
array([['String', 1, 2],
       ['another string', 3, 4]], dtype=object)

When collecting values iteratively, it is usually best to collect them in a list, and make the array afterwards: 迭代地收集值时,通常最好将它们收集在一个列表中,然后再创建数组:

For example, making a list with your data: 例如,列出数据:

In [371]: alist = [("String", 1, 2)]
In [372]: alist.append(("another string", 3, 4))
In [373]: alist
Out[373]: [('String', 1, 2), ('another string', 3, 4)]

For many purposes that list is quite useful, alist[0] , or [i[0] for i in alist] . 对于许多目的来说,list是非常有用的alist[0][i[0] for i in alist] list [i[0] for i in alist]

To make a list, one option is a structured array. 要列出列表,一个选择是结构化数组。 Because I collected values as a list of tuples I can do: 因为我将值收集为元组列表,所以我可以这样做:

In [374]: np.array(alist, dtype='U20,int,int')
Out[374]: 
array([('String', 1, 2), ('another string', 3, 4)], 
      dtype=[('f0', '<U20'), ('f1', '<i4'), ('f2', '<i4')])
In [375]: _['f1']
Out[375]: array([1, 3])

We access fields of such an array by field name. 我们访问fields由字段名这样的一个数组的。 The array itself is 1d, (2,). 数组本身为1d(2)。

If instead we make an object dtype array: 相反,如果我们创建一个object dtype数组:

In [376]: np.array(alist, dtype=object)
Out[376]: 
array([['String', 1, 2],
       ['another string', 3, 4]], dtype=object)
In [377]: _.shape
Out[377]: (2, 3)
In [378]: __[:,1]
Out[378]: array([1, 3], dtype=object)

With this we can access rows and columns. 这样我们就可以访问行和列。 But beware that we don't get the fast numpy calculation benefits with a object array, especially one with mixed types. 但是请注意,使用对象数组,尤其是混合类型的对象数组,无法获得快速的numpy计算优势。

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

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