Re: Grouping pairs - suggested tools

2010-09-22 Thread MRAB
On 20/09/2010 22:42, Astley Le Jasper wrote: I have a list of tuples that indicate a relationship, ie a is related to b, b is related to c etc etc. What I want to do is cluster these relationships into groups. An item will only be associated with a single cluster. Before I started, I wondered i

Re: Grouping pairs - suggested tools

2010-09-22 Thread Arnaud Delobelle
On Sep 22, 8:30 am, Peter Otten <__pete...@web.de> wrote: > Arnaud Delobelle wrote: > > I think I would go for the two-step approach of constructing the graph > > first and then recursively building connected components.  It sounds > > more complicated at first but when you implement it it turns ou

Re: Grouping pairs - suggested tools

2010-09-22 Thread Peter Otten
Arnaud Delobelle wrote: > I think I would go for the two-step approach of constructing the graph > first and then recursively building connected components. It sounds > more complicated at first but when you implement it it turns out quite > simple: > > from collections import defaultdict > from

Re: Grouping pairs - suggested tools

2010-09-21 Thread Arnaud Delobelle
On 21 Sep, 11:13, Peter Otten <__pete...@web.de> wrote: [...] > > A straightforward implementation: > > $ cat group_edges.py > def find_groups(edges): >     lookup = {} # node --> group >     groups = {} # id(group) --> group >     for a, b in edges:               >         if a in lookup:        

Re: Grouping pairs - suggested tools

2010-09-21 Thread Astley Le Jasper
> I think you have the same bug as Alf's code, you never merge existing > groups. Have you tried Arnaud's counterexample? > > By the way, are ('a', 'b') and ('b', 'a') to be considered equivalent for > your problem? > > Peter Hi Peter, Yes. I realise that this doesn't take into account existing

Re: Grouping pairs - suggested tools

2010-09-21 Thread Peter Otten
Astley Le Jasper wrote: > Thanks all. I was playing around with this and came up with another > solution using dictionaries (... comfort zone ...!!) > from operator import itemgetter > > def group_stuff(data): > > group_dic = {} > group_id = 0 > > for line in data: > i = 0

Re: Grouping pairs - suggested tools

2010-09-21 Thread Astley Le Jasper
Thanks all. I was playing around with this and came up with another solution using dictionaries (... comfort zone ...!!) <<< Code >> from operator import itemgetter def group_stuff(data): group_dic = {} group_id = 0

Re: Grouping pairs - suggested tools

2010-09-21 Thread Peter Otten
Astley Le Jasper wrote: > I have a list of tuples that indicate a relationship, ie a is related > to b, b is related to c etc etc. What I want to do is cluster these > relationships into groups. An item will only be associated with a > single cluster. > > Before I started, I wondered if there wa

Re: Grouping pairs - suggested tools

2010-09-21 Thread Alf P. Steinbach /Usenet
* Arnaud Delobelle, on 21.09.2010 11:13: On Sep 21, 7:19 am, "Alf P. Steinbach /Usenet" wrote: * Alf P. Steinbach /Usenet, on 21.09.2010 01:09: * Astley Le Jasper, on 20.09.2010 23:42: I have a list of tuples that indicate a relationship, ie a is related to b, b is related to c etc etc.

Re: Grouping pairs - suggested tools

2010-09-21 Thread Arnaud Delobelle
On Sep 21, 7:19 am, "Alf P. Steinbach /Usenet" wrote: > * Alf P. Steinbach /Usenet, on 21.09.2010 01:09: > > > > > > > * Astley Le Jasper, on 20.09.2010 23:42: > >> I have a list of tuples that indicate a relationship, ie a is related > >> to b, b is related to c etc etc. What I want to do is clus

Re: Grouping pairs - suggested tools

2010-09-21 Thread Astley Le Jasper
Thanks for the feedback both. I'll have a look at these. One of the frustrating things I find about python is that there are so many modules. There have been times when I've spend ages doing something and then some has said, "Oh yeah, there is a module for that". Anyway. Nearly finished. Cheers

Re: Grouping pairs - suggested tools

2010-09-21 Thread Arnaud Delobelle
On Sep 20, 10:42 pm, Astley Le Jasper wrote: > I have a list of tuples that indicate a relationship, ie a is related > to b, b is related to c etc etc. What I want to do is cluster these > relationships into groups.  An item will only be associated with a > single cluster. > > Before I started, I

Re: Grouping pairs - suggested tools

2010-09-20 Thread Ian Kelly
On Tue, Sep 21, 2010 at 12:19 AM, Alf P. Steinbach /Usenet < alf.p.steinbach+use...@gmail.com >wrote: > Uhm, thinking about it (it must have been my unconscious mind doing the > work, it just popped into my head), if you first sort each individual > equivalence relation so that you never have e.g.

Re: Grouping pairs - suggested tools

2010-09-20 Thread Alf P. Steinbach /Usenet
* Alf P. Steinbach /Usenet, on 21.09.2010 01:09: * Astley Le Jasper, on 20.09.2010 23:42: I have a list of tuples that indicate a relationship, ie a is related to b, b is related to c etc etc. What I want to do is cluster these relationships into groups. An item will only be associated with a si

Re: Grouping pairs - suggested tools

2010-09-20 Thread Ian Kelly
On Mon, Sep 20, 2010 at 5:09 PM, Alf P. Steinbach /Usenet < alf.p.steinbach+use...@gmail.com >wrote: > It seems to be the same problem as "equivalence sets". > > This problem was solved early on because e.g. Fortran compilers had to > construct such sets (equivalence partitions of a set). > > I th

Re: Grouping pairs - suggested tools

2010-09-20 Thread Alf P. Steinbach /Usenet
* Astley Le Jasper, on 20.09.2010 23:42: I have a list of tuples that indicate a relationship, ie a is related to b, b is related to c etc etc. What I want to do is cluster these relationships into groups. An item will only be associated with a single cluster. Before I started, I wondered if th

Grouping pairs - suggested tools

2010-09-20 Thread Astley Le Jasper
I have a list of tuples that indicate a relationship, ie a is related to b, b is related to c etc etc. What I want to do is cluster these relationships into groups. An item will only be associated with a single cluster. Before I started, I wondered if there was any particular tool within Python I