您可以尝试以下操作:
import collections
x = np.array([1,1,2,2])
y = np.array([1,1])
m = np.array([10,13,4,6,15,7])
coords = [str(x[i]) + ',' + str(y[i]) for i in range(len(x))]
results = collections.OrderedDict()
for coords,m in zip(coords,m):
if coords not in results:
results[coords] = m
else:
if m < results[coords]:
results[coords] = m
x = np.array([int(key.split(',')[0]) for key,_ in results.items()])
y = np.array([int(key.split(',')[1]) for key,_ in results.items()])
m = np.array([value for _,value in results.items()])
,
我们可以在这里使用熊猫作为更清洁的解决方案-
import pandas as pd
In [43]: df = pd.DataFrame({'x':x,'y':y,'m':m})
In [46]: out_df = df.iloc[df.groupby(['x','y'])['m'].idxmin()]
# Format #1 : Final output as a 2D array
In [47]: out_df.values
Out[47]:
array([[1,6],[1,2],[2,7],4]])
# Format #2 : Final output as three separate 1D arrays
In [50]: X,Y,M = out_df.values.T
In [51]: X
Out[51]: array([1,2])
In [52]: Y
Out[52]: array([1,2])
In [53]: M
Out[53]: array([6,7,4])
本文链接:https://www.f2er.com/3114840.html