how to group by function if one of the group has relationship with another one in the group?

ast nomail at com.invalid
Wed Jul 26 02:05:37 EDT 2017


"Ho Yeung Lee" <jobmattcon at gmail.com> a écrit dans le message de 
news:ef0bd11a-bf55-42a2-b016-d93f3b831860 at googlegroups.com...
> 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)]

Its not clear to me how you build the groups

Why (1,1) is not in group2 since (1,1) is
a neighbor to both (2,3) and (2,4) ?




More information about the Python-list mailing list