如何在 Python 中获取多维 Numpy 数组元素的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22381427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:49:33  来源:igfitidea点击:

How to get type of multidimensional Numpy array elements in Python

pythonarraysnumpytypes

提问by froggy

How can I get the type of a multidimensional array?

如何获取多维数组的类型?

I treat arrays but considering data type: string, float, Boolean, I have to adapt code so I would have to get the type regardless of dimension that can be one two dimensions or more.

我处理数组,但考虑到数据类型: string, float, Boolean,我必须调整代码,因此我必须获得类型,而不管维度可以是一维还是更多维。

Data can be 1d of real, 3D of string ...

数据可以是实数的 1d,字符串的 3D ...

I would like to recover type of Array, is it a real , is it a string is it a boolean ... without doing Array[0] or Array [0][0][0][0] because dimension can be various. Or a way to get the first element of an array whatever the dimensions.

我想恢复数组的类型,它是一个真实的,它是一个字符串还是一个布尔值......不做 Array[0] 或 Array [0][0][0][0] 因为维度可以是多种多样的. 或者一种获取数组第一个元素的方法,无论维度如何。

It works with np.isreal a bit modified , but I don't found equivalent like isastring or isaboolean ...

它适用于 np.isreal 有点修改,但我没有找到像 isastring 或 isaboolean 这样的等效项......

采纳答案by Aaron Hall

Use the dtypeattribute:

使用dtype属性:

>>> import numpy
>>> ar = numpy.array(range(10))
>>> ar.dtype
dtype('int32')

Explanation

解释

Python lists are like arrays:

Python 列表就像数组:

>>> [[1, 2], [3, 4]]
[[1, 2], [3, 4]]

But for analysis and scientific computing, we typically use the numpy package's arrays for high performance calculations:

但是对于分析和科学计算,我们通常使用 numpy 包的数组进行高性能计算:

>>> import numpy as np
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

If you're asking about inspecting the type of the data in the arrays, we can do that by using the index of the item of interest in the array (here I go sequentially deeper until I get to the deepest element):

如果您要检查数组中数据的类型,我们可以通过使用数组中感兴趣的项目的索引来完成(这里我按顺序深入,直到到达最深的元素):

>>> ar = np.array([[1, 2], [3, 4]])
>>> type(ar)
<type 'numpy.ndarray'>
>>> type(ar[0])
<type 'numpy.ndarray'>
>>> type(ar[0][0])
<type 'numpy.int32'>

We can also directly inspect the datatype by accessing the dtypeattribute

我们也可以通过访问dtype属性直接检查数据类型

>>> ar.dtype
dtype('int32')

If the array is a string, for example, we learn how long the longest string is:

例如,如果数组是一个字符串,我们学习最长的字符串有多长:

>>> ar = numpy.array([['apple', 'b'],['c', 'd']])
>>> ar
array([['apple', 'b'],
       ['c', 'd']], 
      dtype='|S5')
>>> ar = numpy.array([['apple', 'banana'],['c', 'd']])
>>> ar
array([['apple', 'banana'],
       ['c', 'd']], 
      dtype='|S6')
>>> ar.dtype
dtype('S6')

I tend not to alias my imports so I have the consistency as seen here, (I usually do import numpy).

我倾向于不别名我的进口,所以我有在这里看到的一致性,(我通常这样做import numpy)。

>>> ar.dtype.type
<type 'numpy.string_'>
>>> ar.dtype.type == numpy.string_
True

But it is common to import numpy as np(that is, alias it):

但它是常见的import numpy as np(即别名):

>>> import numpy as np
>>> ar.dtype.type == np.string_
True

回答by Ankit Aranya

fruits = [['banana'], [1],  [11.12]]

for first_array in range(len(fruits)):
    for second_array in range(len(fruits[first_array])):
        print('Type :', type(fruits[first_array][second_array]), 'data:', fruits[first_array][second_array])

That show the data type of each values.

显示每个值的数据类型。