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
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'], ['
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]>
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
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'
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
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
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([
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',
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
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
(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,
12 matches
Mail list logo