I am having trouble implementing a python algorithm which does the following: (This is part of an attempt to implement a friend of friend algorithm)
Given a list of the form [[a,b],[c,d],[e,f],...] I want to create a new list of the form [[a, a1,a2,a3,...], [b, b1,b2,b3,...], [c, c1,c2,c3,...],...].
An example to make this clearer is something like the following: given a list [[0,1], [0,4], [0,3], [0,423], [1,232], [1,2], [2,444], [2,12]]
I want the output to group all the elements with the first integer so the output would be [[0, 1,4,3,432],[1, 232,2], [2, 444,12]]
Notes: I have sorted the input list according to the first element in each item.
I have been stumped on how to implement this in a somewhat efficient manner for some time now, and would love to get some advice/suggestions as to how to implement this.
P.S. Ultimately I want this to combine all "liked" terms. What i mean is taking the above example, instead of getting the output [[0, 1,4,3,432],[1, 232,2], [2, 444,12]] I would get [[0, 1,4,3,432],[1, 232,2, 444,12]], where the "2" term and its shared elements have joined the elements associated with the "1" term since 1 is associated with 2. This last part may be confusing, but if it makes sense advice would be welcomed as well! Otherwise ignore this last part. =] Thanks again!
Thanks!