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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
Thanks,
JD 12 1611
(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, Oct 17, 2008 at 1:20 PM, JD <Ji*********@gmail.comwrote:
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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
Thanks,
JD
-- http://mail.python.org/mailman/listinfo/python-list
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" <c...@rebertia.comwrote:
(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, Oct 17, 2008 at 1:20 PM, JD <Jiandong...@gmail.comwrote:
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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
Thanks,
JD
-- http://mail.python.org/mailman/listinfo/python-list
On Oct 17, 3:20*pm, JD <Jiandong...@gmail.comwrote:
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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
A = [['a', 'b'], \
['c', 'd'], \
['e', 'f'], \
['a', 'g'], \
['e', 'k'], \
['c', 'u'], \
['b', 'p']]
merged = []
for i in A:
if len(merged)==0:
merged.append(set(i))
else:
gotit = False
for k,j in enumerate(merged):
u = j.intersection(set(i))
if len(u):
merged[k] = j.union(set(i))
gotit = True
if not gotit:
merged.append(set(i))
print merged
##
## [set(['a', 'p', 'b', 'g']), set(['c', 'u', 'd']), set(['k', 'e',
'f'])]
##
>
Thanks,
JD
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', 'p']]
g.addArcs(data, bi=True)
print g.connectedComponents()
# Output: [['a', 'b', 'g', 'p'], ['c', 'u', 'd'], ['e', 'k', 'f']]
Bye,
bearophile
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(['k', 'e',
'd', 'f'])
The right result should be:
['a', 'p', 'b', 'g'], ['c', 'u', 'e', 'd', 'k', 'f']
JD
On Oct 17, 3:00 pm, Mensanator <mensana...@aol.comwrote:
On Oct 17, 3:20 pm, JD <Jiandong...@gmail.comwrote:
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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
A = [['a', 'b'], \
['c', 'd'], \
['e', 'f'], \
['a', 'g'], \
['e', 'k'], \
['c', 'u'], \
['b', 'p']]
merged = []
for i in A:
if len(merged)==0:
merged.append(set(i))
else:
gotit = False
for k,j in enumerate(merged):
u = j.intersection(set(i))
if len(u):
merged[k] = j.union(set(i))
gotit = True
if not gotit:
merged.append(set(i))
print merged
##
## [set(['a', 'p', 'b', 'g']), set(['c', 'u', 'd']), set(['k', 'e',
'f'])]
##
Thanks,
JD
Thanks,
This one really works.
JD
On Oct 17, 3:17 pm, bearophileH...@lycos.com 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 = Graph()
data = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'], ['e', 'k'],
['c', 'u'], ['b', 'p']]
g.addArcs(data, bi=True)
print g.connectedComponents()
# Output: [['a', 'b', 'g', 'p'], ['c', 'u', 'd'], ['e', 'k', 'f']]
Bye,
bearophile
On Oct 17, 3:20*pm, JD <Jiandong...@gmail.comwrote:
Hi,
I need help for a task looks very simple:
<snip>
I smell "homework assignment".
-- Paul
On Oct 17, 4:34*pm, JD <Jiandong...@gmail.comwrote:
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(['k', 'e',
'd', 'f'])
The right result should be:
['a', 'p', 'b', 'g'], ['c', 'u', 'e', 'd', 'k', 'f']
JD
On Oct 17, 3:00 pm, Mensanator <mensana...@aol.comwrote:
On Oct 17, 3:20 pm, JD <Jiandong...@gmail.comwrote:
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 share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Well, you should have asked for that, if that's what you wanted.
>
Anyone has a solution?
A = [['a', 'b'], \
* * *['c', 'd'], \
* * *['e', 'f'], \
* * *['a', 'g'], \
* * *['e', 'k'], \
* * *['c', 'u'], \
* * *['b', 'p']]
A.sort()
merged = []
for i in A:
* * if len(merged)==0:
* * * * merged.append(set(i))
* * else:
* * * * gotit = False
* * * * for k,j in enumerate(merged):
* * * * * * u = j.intersection(set(i))
* * * * * * if len(u):
* * * * * * * * merged[k] = j.union(set(i))
* * * * * * * * gotit = True
* * * * if not gotit:
* * * * * * merged.append(set(i))
print merged
##
## * *[set(['a', 'p', 'b', 'g']), set(['c', 'u', 'd']), set(['k', 'e',
'f'])]
##
## [set(['a', 'p', 'b', 'g']), set(['c', 'e', 'd', 'f', 'u', 'k'])]
>
Thanks,
JD
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 the list share the same name, all these nodes need
be merged.
For example, ['a', 'b'], ['a', 'g'] ['b', 'p'] will be merged to ['a',
'b', 'g', 'p']
The answer should be:
[['a', 'b', 'g', 'p'], ['c', 'd', 'u'], ['e', 'f', 'k']]
Anyone has a solution?
data = [
['a', 'b'],
['c', 'd'],
['e', 'f'],
['a', 'g'],
['e', 'k'],
['c', 'u'],
['b', 'p'],
]
def merge(inlist):
n = len(inlist)
outlist = [set(inlist.pop())]
while inlist:
s = set(inlist.pop())
merged = False
for i in range(len(outlist)):
t = outlist[i]
u = s | t
if len(u) < len(s) + len(t):
outlist[i] = u
merged = True
break
if not merged:
outlist.append(s)
if len(outlist) == n:
return outlist
else:
return merge(outlist)
for s in merge(data):
print s
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 <pt...@austin.rr.comwrote:
On Oct 17, 3:20 pm, JD <Jiandong...@gmail.comwrote:Hi,
I need help for a task looks very simple:
<snip>
I smell "homework assignment".
-- Paul
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'], ['e', 'k'], ['c', 'u'], ['b', 'p']] g.addArcs(data) g.connectedComponents()
[['a', 'b', 'g', 'p'], ['c', 'u', 'd'], ['e', 'k', 'f']]
Bye,
bearophile
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
a_net |= r[b]
for x in r[b]:
r[x] = a_net
else: # add a single node to the net
a_net.add(b)
r[b] = a_net
elif b in r: # add this node to another net
r[b].add(a)
r[a] = q[b]
else: # create a new net
r[a] = r[b] = set([a, b])
# r holds the result nets, but each net is in values multiple times
# So, get the unique elements in r's values.
return dict((id(x), x) for x in r.values()).values()
for group in do_nets([['a', 'b'], ['c', 'd'], ['e', 'f'], ['a', 'g'],
['e', 'k'], ['c', 'u'], ['b', 'p']]):
print group
--Scott David Daniels Sc***********@Acm.Org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Luke Airig |
last post by:
I have two xml files that I need to merge on their common field
(date_time). The merged output file needs to have the date_time field
and all...
|
by: Andreas Kasparek |
last post by:
Hola!
I'm preparing my master thesis about a XML Merge Tool implementation and was
wondering if there is any open standard for XML diff regarding...
|
by: anunaygupta |
last post by:
Hello all,
I have a data structures problem.
Assume there are 2 linked lists L1 and L2. They merge into some node.
L1 1->2->3->4->5->6
/
L2...
|
by: Meelis Lilbok |
last post by:
Hi is for synchronizing two xml files any fast solution?
Lets say i have 2 xml files 1.xml and 2.xml
1.xml contianes
<test>
<t...
|
by: incredible |
last post by:
how to merge two sorted link list
|
by: Zeba |
last post by:
Hi,
I have to write program in C# to merge sort a linked list (doubly
linked). Is it true that the best way to do it is copy it into an
array,...
|
by: Sam Durai |
last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes
even 0 rows) to a big table (of rows around 4 billion). I used the PK
of the...
|
by: vekka |
last post by:
Hi!
Could someone please help me to understand the use of merge algorithm(conceptually) with linked lists. What I want to do in the linked list...
|
by: blackirish |
last post by:
Hi all,
I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |