如何从列表中的每个numpy数组中提取最大值?

2024-04-18 17:54:48 发布

您现在位置:Python中文网/ 问答频道 /正文

实际上,我对python非常陌生,正在研究一些图像问题语句。陷入了一个问题而无法摆脱。你知道吗

我有这样的数据帧:

Image                       RGB                 max_1 max_2 max_3
file1   [[224,43,234][22,5,224][234,254,220]]     234   224   254
file2   [[22,143,113][221,124,224][234,254,123]]  143   224   254
file3   [[44,45,2][2,5,4][34,254,220]]             45     5   254
file4   [[224,243,34][22,5,24][24,25,20]]         243    24    25
file5   [[210,13,34][22,5,224][234,254,220]]      210   224   254

我尝试了np.max(),但是它给了我意想不到的结果,这意味着对于第一行,它给了我输出254,以此类推。你知道吗

我希望输出为列max\u 1、max\u 2和max\u 3。你知道吗


Tags: 数据图像imagenprgb语句file1max
3条回答

我假设你分别想要R,G和B的最大值。 如果你想这样做,这里有一个方法:

a = np.array([ [224,43,234], [22,5,224], [234,254,220]])
r_max = a.T[0].max()
g_max = a.T[1].max()
b_max = a.T[2].max()

你可以这样做:

file1 = [[224,43,234],[22,5,224],[234,254,220]]

for idx, inner_list in enumerate(file1):
    print('max_'+str(idx+1)+' : '+str(max(inner_list)))

使用list-comprehension

a = np.array([[224,43,234], [22,5,224], [234,254,220]])

print([x.max() for x in a])

输出

[234, 224, 254]

相关问题 更多 >