python - numpy.array Boolean mix with int
问题描述
When I create the numpy.array
which has bool
type mixed with int
np.array([True,True,100])
Out[656]: array([ 1, 1, 100])
np.array([True,True,100]).dtype
Out[657]: dtype('int32')
It will converted whole array to type int, I guess maybe the int class is higher than bool which is make sense.
And, If I already have a bool
type array
as below :
#When I assign the value by using the index
b=np.array([True,True,False])
b[2]
Out[659]: False
b[2]=100
b
Out[661]: array([ True, True, True])
It will treat the 100 to be bool
True
, this is make sense too.
However it confused me when I consider both of the situations.
Could you please explain this it a little bit.
Thank you so much.
Wen
解决方案
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
import numpy as np
np.array([True,True,100])
Out[3]: array([ 1, 1, 100])
np.array([True,True,100], dtype=bool)
Out[4]: array([ True, True, True])
您应该dtype
为数组使用所需的数据类型。
请检查文件
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.array.html
对于你的第二个问题,请检查dtype
参数的描述,
“如果没有给出,那么类型将被确定为保持序列中对象所需的最小类型。”
您创建不带dtype
参数的数组,并且保存序列对象的值类型的最小值是bool
,当您分配一个int
值时,它将更改为bool
。
推荐阅读
- flutter - Flutter:如何创建自定义着色器
- javascript - 使用带有对象或单个值的钩子?
- c - 如何使用C找到网络接口的底层设备节点?
- javascript - 你需要在switch语句JS中使用默认值吗
- csv - 在jmeter性能测试中重构summery.csv文件
- kendo-ui-angular2 - 剑道角图未捕获类型错误:无法读取未定义的属性“createCrosshairTooltips”
- javascript - 使用预加载的服务器数据为 Angular 应用程序提供服务
- python - 如何在 Python 中管理全局变量
- java - Java使用递归将输入更改为大写
- vue.js - 将 Vue 代码的静态部分从 methods() 移动到
Within my code there is some static data like
const YAMAP_KEY
andlet src
in the<script>
section. I'd like to move those to the<template>