1
UPDATE1: 如果相同的颜色是类似的邻居连接然后将它们分组如果组中的任何一个成员与另一组中的任何一个组成员有邻居关系,如何按功能分组?
如何通过如果组成员中的任何一个具有与另一组中的组成员的任何一个相邻关系功能组?
if x coordinate same and y coordinate difference is 1 then return 1 #same memeber
if y coordinate same and x coordinate difference is 1 then return 1 #same memeber
else return 0 #not group memeber
回溯(最近通话最后一个): 文件 “”,1号线,在 类型错误:isneighborlocation()到底需要2个参数(1给出)
from itertools import groupby
testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
def isneighborlocation(lo1, lo2):
if abs(lo1[0] - lo2[0]) == 1 or lo1[1] == lo2[1]:
return 1
elif abs(lo1[1] - lo2[1]) == 1 or lo1[0] == lo2[0]:
return 1
else:
return 0
groupda = groupby(testing1, isneighborlocation)
for key, group1 in groupda:
print key
for thing in group1:
print thing
expect output 3 group
group1 [(1,1)]
group2 [(2,3),(2,4)]
group3 [(3,5),(3,6),(4,6)]
我不知道什么是中心,实际上没有中心,如果连接了相同的颜色然后对它进行分组,那么只要这个目标,如何使用groupby的部分函数,你能明确地写出解决方案吗? – user353573