【问题标题】:Convert uint8 to int64 in python在python中将uint8转换为int64
【发布时间】:2017-09-22 11:47:01
【问题描述】:

我有一个 uint8 类型的变量(它只有两个值,0 和 1),我想用 -1 替换零。我是否必须首先将此变量转换为 int64(或任何允许负数的整数数据类型)?如果是这样,我该如何实现?

【问题讨论】:

  • 你能把它设为 255 吗?
  • 你有uint8是什么意思,你使用的是ctypes还是numpy?
  • Python 没有不同的整数类型,所以我不确定您指的是哪种类型。你应该展示一些你在做什么的代码。
  • 我刚刚尝试用-1替换0,但它用255替换了它
  • 我正在使用 numpy

标签: python numpy type-conversion


【解决方案1】:

您不需要 int64。 int8 应该可以。将 uint8 转换为 int8 :

import numpy as np
x = np.uint8(-1) # -1 does not exist in this coding, this is just a test
x
> 255
np.int8(x)
> -1 # eureka 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2013-02-04
    相关资源
    最近更新 更多