python - 使用字典映射 numpy 数组?

标签 python numpy dictionary

我有一个可能很长的 1 和 0 数组,如下所示: a = [0,0,1,0,1,0,.......] 我想将每对连续的值转换为 0 和 3 之间的整数,如下所示:

  • 0,0 -> 0
  • 0,1 -> 1
  • 1,0 -> 3
  • 1,1 -> 2

我正在寻找一种干净高效的方法来使用上面的映射创建数组 b(下面的示例输出): b = [0, 3, 3,...]

字典是个好主意吗?不过我不知道该怎么做。

最佳答案

尝试:

x = np.reshape(a, (-1,2))

b = x[:,0]*2 + (x[:,0] ^ x[:,1])

关于python - 使用字典映射 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65943214/

相关文章:

python - 将python for循环转换为while循环

python re 模块 - 用于提取文本片段的正则表达式

python - 得到结果后编辑输出

python - 将椭圆拟合到python中的一组数据点

python - numpy.searchsorted 用于包含 numpy.nan 的数组

python - 用 OrderedDict 替换对象的默认 __dict__

python - 如何根据键的第一个元素(元组)对字典进行排序

python - 安装石榴时遇到问题

python - 如何获得 numpy 掩码数组中的掩码行数?

dictionary - 如何检查 map 是否包含 Go 中的键?