【问题标题】:create a huge array in python [closed]在python中创建一个巨大的数组[关闭]
【发布时间】:2020-12-12 14:25:08
【问题描述】:

我想创建一个像这样包含的二维数组。

每个元组都包含三个代表着色 RGB 系统的数字

[
(0,0,0) (0,0,1) (0,0,2)  (0,0,3)  (0,0,4)  (0,0,5)  (0,0,6)  (0,0,7)

(0,0,8) (0,0,9) (0,0,10) (0,0,11) (0,0,12) (0,0,13) (0,0,14) (0,0,15)
 
(0,0,16) (0,0,17) (0,0,18)  (0,0,19)  (0,0,20)  (0,0,21)  (0,0,22)  (0,0,23) 
.....    
(250,250,242) (250,250,243) (250,250,244) .... (250,250,250)                                                    
]

相同,但对于普通 RGB,编号 .this 数组,例如 8x8 并为每个引用此颜色名称的元组提供一个键 例如,像 (0,0,0) => 黑色

-- 我试过..并且确实喜欢这个

arr2 = np.array([(i,i,i) for i in range(250)] , dtype = [('Red','i2'),('Green','i2'),('Blue','i2')])
print(arr2)

但这没有用!

【问题讨论】:

  • 您的问题到底是什么?您是否已经尝试实现此功能?你遇到了什么问题?
  • (0,0,2)'not_quite_that_black'?所以你有 1600 万并更改颜色名称?这有什么用?您的代码只创建 (0,0,0),(1,1,1),...,(249,249,249) 因为它只使用一个运行变量...
  • 对于第一个数组,不需要名称。只是我需要数据,作为二维数组。我知道我的代码是错误的,但我已经达到了。

标签: python arrays pandas numpy


【解决方案1】:

您可以使用 list comprehension

>>> colors = [(r, g, b) for r in range(256) for g in range(256) for b in range(256)]
>>> colors[:10]     # First 10 colors
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5), (0, 0, 6), (0, 0, 7), (0, 0, 8), (0, 0, 9)]
>>> colors[-10:]    # Last 10 colors
[(255, 255, 246), (255, 255, 247), (255, 255, 248), (255, 255, 249), (255, 255, 250), (255, 255, 251), (255, 255, 252), (255, 255, 253), (255, 255, 254), (255, 255, 255)]
>>> len(colors)     # Numbers of colors in RGB    
16777216

【讨论】:

    【解决方案2】:

    您可以尝试使用字典。

    dict = {(0, 0, 0): "black", ... }
    

    然后您可以像这样使用 RGB 搜索颜色。

    print(dict[(0, 0, 0)])
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2020-08-29
      • 2017-07-08
      • 2019-06-23
      • 2015-09-02
      • 2021-10-16
      相关资源
      最近更新 更多