如何确定numpy数组是否包含整数? [英] How do I find out if a numpy array contains integers?

查看:75
本文介绍了如何确定numpy数组是否包含整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个简单的解决方案,但目前似乎找不到.

I know there is a simple solution to this but can't seem to find it at the moment.

给出一个numpy数组,我需要知道该数组是否包含整数.

Given a numpy array, I need to know if the array contains integers.

仅凭本身检查dtype是不够的,因为存在多个int dtypes(int8,int16,int32,int64 ...).

Checking the dtype per-se is not enough, as there are multiple int dtypes (int8, int16, int32, int64 ...).

推荐答案

numpy书中找到了它!第23页:

Found it in the numpy book! Page 23:

层次结构中的其他类型定义了特定的类型类别. 这些类别对于测试对象是否有用很有用. self.dtype.type返回的属于特定类(使用issubclass).

The other types in the hierarchy define particular categories of types. These categories can be useful for testing whether or not the object returned by self.dtype.type is of a particular class (using issubclass).

issubclass(n.dtype('int8').type, n.integer)
>>> True
issubclass(n.dtype('int16').type, n.integer)
>>> True

这篇关于如何确定numpy数组是否包含整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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