Re: algorizm to merge nodes

2008-10-18 Thread Scott David Daniels
JD wrote: It could be a very good "homework assignment". This is for a real application. def do_nets(pairs): r = {} for a, b in pairs: if a in r: a_net = r[a] if b not in a_net: # if not redundant link if b in r: # Need to merge nets

Re: algorizm to merge nodes

2008-10-17 Thread bearophileHUGS
JD: > Thanks, > This one really works. Note that you can save some RAM (almost half) creating a directed graph, because later the connectedComponents() manages the arcs as undirected anyway: >>> from graph import Graph >>> g = Graph() >>> data = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['

Re: algorizm to merge nodes

2008-10-17 Thread JD
It could be a very good "homework assignmet". This is for a real application. each item in the list represent two terminals of a resistor. All the resistors in the list are shorted, I need to figure out how many independent nets are there. JD On Oct 17, 4:16 pm, Paul McGuire <[EMAIL PROTECTED]>

Re: algorizm to merge nodes

2008-10-17 Thread Gerard flanagan
JD wrote: Hi, I need help for a task looks very simple: I got a python list like: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'], ['c', 'u'], ['b', 'p']] Each item in the list need to be merged. For example, 'a', 'b' will be merged, 'c', 'd' will be merged. Also if the node in

Re: algorizm to merge nodes

2008-10-17 Thread Mensanator
On Oct 17, 4:34 pm, JD <[EMAIL PROTECTED]> wrote: > Hi, > > Thanks, > > It works for this example, > > but if I add another item ['e', 'd']: > [['a', 'b'], \ >      ['c', 'd'], \ >      ['e', 'f'], \ >      ['a', 'g'], \ >      ['e', 'k'], \ >      ['c', 'u'], \ >      ['b', 'p'],\ >      ['e', 'd'

Re: algorizm to merge nodes

2008-10-17 Thread Paul McGuire
On Oct 17, 3:20 pm, JD <[EMAIL PROTECTED]> wrote: > Hi, > > I need help for a task looks very simple: > I smell "homework assignment". -- Paul -- http://mail.python.org/mailman/listinfo/python-list

Re: algorizm to merge nodes

2008-10-17 Thread JD
Thanks, This one really works. JD On Oct 17, 3:17 pm, [EMAIL PROTECTED] wrote: > JD, you probably need the algorithm for connected components of an > undirected graph. > > For example you can do that with my graph > lib:http://sourceforge.net/projects/pynetwork/ > > from graph import Graph > g

Re: algorizm to merge nodes

2008-10-17 Thread JD
Hi, Thanks, It works for this example, but if I add another item ['e', 'd']: [['a', 'b'], \ ['c', 'd'], \ ['e', 'f'], \ ['a', 'g'], \ ['e', 'k'], \ ['c', 'u'], \ ['b', 'p'],\ ['e', 'd']] The result is set(['a', 'p', 'b', 'g']), set(['e', 'c', 'u', 'd']), set([

Re: algorizm to merge nodes

2008-10-17 Thread bearophileHUGS
JD, you probably need the algorithm for connected components of an undirected graph. For example you can do that with my graph lib: http://sourceforge.net/projects/pynetwork/ from graph import Graph g = Graph() data = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'], ['c', 'u'], ['b',

Re: algorizm to merge nodes

2008-10-17 Thread Mensanator
On Oct 17, 3:20 pm, JD <[EMAIL PROTECTED]> wrote: > Hi, > > I need help for a task looks very simple: > > I got a python list like: > > [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'], ['c', > 'u'], ['b', 'p']] > > Each item in the list need to be merged. > > For example, 'a', 'b' will

Re: algorizm to merge nodes

2008-10-17 Thread JD
Hi, Thanks for the help, but the result is not quite right: [['a', 'b', 'g'], ['c', 'd', 'u'], ['b', 'p'], ['e', 'f', 'k']] the ['b', 'p'] is not merged. JD On Oct 17, 2:35 pm, "Chris Rebert" <[EMAIL PROTECTED]> wrote: > (Disclaimer: completely untested) > > from collections import defaultdic

Re: algorizm to merge nodes

2008-10-17 Thread Chris Rebert
(Disclaimer: completely untested) from collections import defaultdict merged = defaultdict(list) for key, val in your_list_of_pairs: merged[key].append(val) result = [[key]+vals for key, vals in merged.items()] Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com On Fri,

algorizm to merge nodes

2008-10-17 Thread JD
Hi, I need help for a task looks very simple: I got a python list like: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'], ['c', 'u'], ['b', 'p']] Each item in the list need to be merged. For example, 'a', 'b' will be merged, 'c', 'd' will be merged. Also if the node in the list sha