将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here:

Weird behaviour initializing a numpy array of string data
                                
                                    (4个答案)
                                
                        
                                2年前关闭。
            
                    
我编写的代码遍历了低分辨率图像的每个像素,并将每个像素的色相,饱和度和亮度级别(HSL)存储在数组中。现在,我想遍历所有这些HSL级别,以确定它最接近哪种颜色,并将此信息存储到一个新数组中。我写了这段代码:

arrayCol = np.zeros((sHeight,sWidth), dtype = str)
for y in range(sHeight):
    for x in range(sWidth):
        #for n in range(3):
        if arrayHSL[y][x][2] <= 10:
            if arrayHSL[y][x][2] <= 10:
                arrayCol[y][x] = 'blk'

print(arrayCol)

如果亮度低于特定阈值,则将像素分配为“ blk”或“ black”。但是,在打印和查看数组时,没有在正确的位置显示'blk',而是仅显示输出中看到的第一个字母'b':

[['' '' '' '' '' 'b' 'b' 'b' 'b' 'b' 'b' 'b' 'b' 'b' '' 'b' '' '' '' '']
 ['' '' '' '' '' '' 'b' 'b' 'b' 'b' '' 'b' 'b' '' '' '' '' '' '' '']
 ['b' '' '' '' '' '' '' '' 'b' 'b' 'b' 'b' 'b' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' 'b' 'b' 'b' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '' 'b' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' 'b' '' '' '' '' '' 'b' '' '' '' '' '' '' '' '']
 ['' 'b' '' 'b' '' '' '' '' '' '' 'b' 'b' '' '' '' '' '' '' '' '']
 ['' '' '' 'b' '' 'b' '' '' '' '' '' '' '' 'b' '' '' '' '' '' '']
 ['' '' 'b' '' '' 'b' 'b' 'b' '' '' '' '' '' 'b' '' '' '' '' '' '']
 ['' '' 'b' 'b' '' 'b' '' 'b' '' 'b' 'b' '' 'b' 'b' '' '' '' '' '' '']
 ['b' '' 'b' '' 'b' '' '' '' 'b' 'b' '' 'b' 'b' 'b' '' '' '' 'b' '' '']
 ['b' 'b' 'b' 'b' 'b' 'b' 'b' '' '' '' '' 'b' '' 'b' '' '' '' '' '' '']

将名称“ blk”更改为其他名称(例如“ rlk”)的操作相同,仅显示“ r”。这不是什么大问题,但是一旦我开始实现其他颜色(例如蓝色或棕色),我和Python都将无法区分它们。所以我的问题是如何获取数组来存储颜色的全名,而不仅仅是第一个字母?谢谢

python参考方案

如果键入arrayCol.dtype,则应打印dtype('S1'),表示长度为1的字符串。
您可以为固定长度指定dtype='s256'或为任意长度字符串指定dtype=object

arrayCol = np.zeros((sHeight,sWidth), dtype = object)

如何根据子列表的长度对列表列表进行排序[重复] - python

This question already has answers here: Sorting Python list based on the length of the string (7个答案) 5年前关闭。 我有以下清单a = [['a', 'b', 'c'], ['d'…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

为随机选择的变量分配一个值[重复] - python

This question already has answers here: How do I randomly select a variable from a list, and then modify it in python?                                                              …

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…