Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中的每个numpy数组中提取最大值?_Python_Numpy - Fatal编程技术网

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

Python 如何从列表中的每个numpy数组中提取最大值?,python,numpy,Python,Numpy,实际上,我对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 f

实际上,我对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_1、max_2和max_3。

您可以这样做:

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)))

我假设你分别想要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()
另一种方式:

import numpy as np

a=np.array([[1,2,3],[11,12,13],[21,22,23]])
print(a)

maxInRows = np.amax(a, axis=1)
print('Max value of every Row: ', maxInRows)

你说你有一个数据帧,所以我假设它是一个
pandas
DataFrame
对象。 在这种情况下,您可以使用列表理解从列表中的每个子列表中获取最大值,并将每个元素分配给一个新列(此循环并不优雅,但会起作用):


使用
列表理解

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

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

[234, 224, 254]

像这样解决了这个问题。谢谢你给我所有的答案

df['max_1'] = 0
df['max_2'] = 0
df['max_3'] = 0
for i in range(5):
    df['max_1'][i] = df['RGB'][i][0].max()
    df['max_2'][i] = df['RGB'][i][1].max()
    df['max_3'][i] = df['RGB'][i][2].max()

您希望得到什么输出?可以使用axis获取最大值,使用axis=0从列中获取最大值,或者使用asix=1从行中获取最大值。
df['max_1'] = 0
df['max_2'] = 0
df['max_3'] = 0
for i in range(5):
    df['max_1'][i] = df['RGB'][i][0].max()
    df['max_2'][i] = df['RGB'][i][1].max()
    df['max_3'][i] = df['RGB'][i][2].max()