我有一个10x20 numpy数组
[[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255]]
我希望以一系列维度的形式重复数字0(0和39的岛)。如上例所示,如果我将数字0
作为参数传递,则函数应从上到下穿过矩阵,然后从左到右,从而产生维数组。以上示例的输出应为
#Zeroes repetition dimensions
[[20,4],[3,5],[2,5]]
如何获得此输出。任何numpy函数来做到这一点?提前谢谢。
答案 0 :(得分:1)
我会使用itertools groupby
:
import numpy as np
from itertools import groupby
from collections import Counter
arr=np.array([[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255],
[255,255,255,255,255,255,255,0,0,0,255,255,0,0,255,255,255,255,255,255]])
val_to_check=0
agg_res=[]
for a in arr:
groups = groupby(a)
temp = [[label, sum(1 for _ in group)] for label, group in groups]
for t in temp:
if t[0]==val_to_check:
agg_res.append(t)
res_to_group=[l[1] for l in agg_res]
final_res=Counter(res_to_group)
#or, in list form:
res_list=map(list,final_res.items())
输出:
[[2, 5], [3, 5], [20, 4]]
或者,如果你想要,作为一个功能:
def find_islands(arr,val):
agg_res=[]
for a in arr:
groups = groupby(a)
temp = [[label, sum(1 for _ in group)] for label, group in groups]
for t in temp:
if t[0]==val:
agg_res.append(t)
res_to_group=[l[1] for l in agg_res]
final_res=Counter(res_to_group)
#or, in list form:
return map(list,final_res.items())
示例运行:
In[65]: find_islands(arr,255)
Out[65]: [[2, 5], [20, 1], [6, 5], [7, 5]]