【问题标题】:Array of Index to colors to array of colors in python [duplicate]python中颜色数组的颜色索引数组[重复]
【发布时间】:2019-05-01 16:32:02
【问题描述】:

有一个带有颜色索引的数组 (2d) 有没有办法用它的颜色一步替换索引而不是逐个元素?

例子:

colors=[[255,0,0],[125,222,11]]
im_x=[[0,0],[1,0]]

#the result must be:

im_c= [[[255,0,0],[255,0,0]],[[125,222,11],[255,0,0]]

我试过了 颜色[im_x] 但它不起作用:(

【问题讨论】:

  • 问题是你没有定义数组...
  • 你能说得具体一点吗?

标签: python arrays numpy


【解决方案1】:

将数组定义为 numpy 数组,然后按照您的尝试对它们进行索引:

import numpy as np
colors = np.array([[255,0,0],[125,222,11]])
im_x = np.array([[0,0],[1,0]])

colors[im_x]

array([[[255,   0,   0],
        [255,   0,   0]],

       [[125, 222,  11],
        [255,   0,   0]]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 2015-01-11
    • 2023-03-25
    • 2018-12-27
    • 1970-01-01
    • 2011-01-22
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多