0

Possible Duplicate:
I want to group tuples based on similar attributes

I am very new to Python and coding in general, have a list of cluster pairs like so

[[1,2],[3,4],[2,3],[4,5]......],

I have written this code to join the cluster pairs into list of larger clusters.

for i in range (len(Clist)):
    for j in range(i+1,len(Clist)):
        for disk in Clist[i]:
            if disk in Clist[j]:
                joined=joincluster(Clist[i], Clist[j])
                Clist[i]=list(set(joined))
                Clist[j]=[]
                break

I go on to take the empty ones out. However it doesn't join all the pairs up when i have more then 30 or so cluster pairs, some clusters that should have been joined have not been. I can't see where I'm going wrong? Can anybody me identify the problem?

[[0, 2], [0, 9], [1, 10], [2, 0], [2, 9], [3, 6], [3, 10], [3, 11], [4, 5], [4, 7], [4, 12], [4, 14], [5, 4], [5, 8], [5, 12], [5, 14], [6, 3], [6, 10], [6, 11], [7, 4], [7, 12], [8, 5], [8, 14], [9, 0], [9, 2], [9, 13], [10, 1], [10, 3], [10, 6], [10, 11], [11, 3], [11, 6], [11, 10], [12, 4], [12, 5], [12, 7], [13, 9], [14, 4], [14, 5], [14, 8]]

Sorry, problem only happens when i have a fairly large number of pairs. And this is what I get

[[0, 9, 2, 13], [11, 1, 10, 3, 6], [3, 6], [4, 5, 7, 8, 12, 14]]

so the 3,6 there should have been merged but it hasn't.

Community
  • 1
  • 1
  • 7
    Could you add a sample input and output ? – asheeshr Jan 19 '13 at 15:04
  • How exactly do you want to merge them? We need to see some logic behind your input and output, which clusters do you want to merge with which? –  Jan 19 '13 at 15:13
  • Well i just want to merge the lists, that contain the same numbers, so for example [1,2] gets merged with [2,8] to make [1,2,8]. Im not sure what you mean by where, but the merged lists should be in the same original list that the clusters were in, if its easier, in a completely new list. –  Jan 19 '13 at 15:16
  • Is this an assignment question ? – asheeshr Jan 19 '13 at 15:20
  • You need to provide more details on what you actually want to achieve. What are the exact rules? – dzida Jan 19 '13 at 20:42

1 Answers1

1

Let's take a simpler example, with only 4 clusters like so:

clusters = [ [1, 10], [1, 20], [3, 6], [3, 10] ]

These 4 are enough to show why [3, 6] gets separated from all other clusters. You loop through these clusters starting from first to last. Here's what happens:

  1. Is there already a cluster that has 1 or 10 in it? - No, create a new one including both. We now have the following: [[1, 10]].
  2. Is there already a cluster that has 1 or 20 in it? - Yes, append to the match. We now have the following: [[1, 10, 20]].
  3. Is there already a cluster that has 3 or 6 in it? - No, create a new one including both. We now have the following: [[1, 10, 20], [3, 6]].
  4. Is there already a cluster that has 3 or 10 in it? - Yes, append to the match. We now have the following: [[1, 3, 10, 20], [3, 6]].

Problem is: after appending [3, 10] to [1, 10], it doesn't check if there are existing clusters that have 3 in them already, which don't have 10. You need to add that to your code and it will work fine.