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行,in TypeError: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)]
答案 0 :(得分:0)
groupby
中的函数参数接受单个参数,因此您必须使用部分函数来添加“额外”参数。
https://docs.python.org/3/library/functools.html#functools.partial
from functools import partial
...
groupda = groupby(testing1, partial(isneighborlocation, centre))
但是,当然,这意味着你必须明确地测试每个'中心' 顺便说一句,要注意groupby上的排序要求 - 它似乎可能会让你失望。