3

Well

I have a unique combination of elements (A B C D E F)

from itertools import combinations

data = ['A', 'B', 'C', 'D', 'E', 'F'];
comb = combinations(data, 2);

d = [];
for i in comb:
    d.append([i[0], i[1]]);

print d

This returns to me:

[['A', 'B'], 
 ['A', 'C'], 
 ['A', 'D'],
 ['A', 'E'],
 ['A', 'F'],
 ['B', 'C'],
 ['B', 'D'],
 ['B', 'E'],
 ['B', 'F'],
 ['C', 'D'],
 ['C', 'E'],
 ['C', 'F'],
 ['D', 'E'],
 ['D', 'F'],
 ['E', 'F']]

The question is, how to sort this in a way that the line N do not repeat element [0] or element [1] of line (N-1)...in a simpler way:

AB (This line can have any element)
CD (This line can't have A or B)
EF (This line can't have C or D)
AC (This line can't have E or F)
...
Edek
  • 33
  • 3
  • 1
    You don't need to put semicolons at the end of statements (and you shouldn't). Also, you can generate the combinations list `d` simply using `d = map(list, combinations(data, 2))`. – Jayanth Koushik Jun 03 '14 at 06:30
  • @JayanthKoushik Why shouldn't use semicolons? And Thanks :D – Edek Jun 03 '14 at 06:51
  • @sundarnatarajサンダーナタラジ Sorry for taking your time, I just started to think that is impossible to organize in the way that I want...now I`m trying to do some math calcs to verify this... – Edek Jun 03 '14 at 06:54
  • @user3701822: You shouldn't use semicolons because it serves no purpose and violates python style conventions. It's frowned upon... – Jayanth Koushik Jun 03 '14 at 07:03
  • @JayanthKoushik ah, I normally use because two reasons, one is because a habit of C and PHP, the other reason is because somethimes I code multiples things in same line... – Edek Jun 03 '14 at 07:12

2 Answers2

4
mylist= [['A', 'B'],
 ['A', 'C'], 
 ['A', 'D'],
 ['A', 'E'],
 ['A', 'F'],
 ['B', 'C'],
 ['B', 'D'],
 ['B', 'E'],
 ['B', 'F'],
 ['C', 'D'],
 ['C', 'E'],
 ['C', 'F'],
 ['D', 'E'],
 ['D', 'F'],
 ['E', 'F']] 


a=mylist[:] #this used to assign all elements to a so u have ur mylist safe    
b=[]
b.append(a[0]) #this appends the first list in the list
del a[0]  #now deleting appended list        
while len(a)>0:
    for val,i in enumerate(a):# enumerte gives index and value of list
        if len(set(b[len(b)-1]).intersection(set(i)))==0: # this checks intersection so that both list should not have same elements
            b.append(a[val])
            del a[val]
print b

#output [['A', 'B'], ['C', 'D'], ['E', 'F'], ['A', 'C'], ['B', 'D'], ['C', 'E'], ['D', 'F'], ['A', 'E'], ['B', 'C'], ['D', 'E'], ['A', 'F'], ['B', 'E'], ['C', 'F'], ['A', 'D'], ['B', 'F']]
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
2

Using the neighborhood generator from this answer you can get the previous, current and next element in your loop, so that you can compare them. Then you can do something like this

from itertools import combinations

# Credit to Markus Jarderot for this function
def neighborhood(iterable):
    iterator = iter(iterable)
    prev = None
    item = iterator.next()  # throws StopIteration if empty.
    for next in iterator:
        yield (prev,item,next)
        prev = item
        item = next
        # this can be written like this also prev,item=item,next
    yield (prev,item,None)

data = ['A', 'B', 'C', 'D', 'E', 'F'];
comb = combinations(data, 2);

d = [];
for prev, item, next in neighborhood(comb):
    # If prev and item both exist and neither are in the last element in d
    if prev and item and not any(x in d[-1] for x in item):
        d.append([item[0], item[1]])
    elif item and not prev: # For the first element
        d.append([item[0], item[1]])

print d

This prints

[['A', 'B'],
 ['C', 'D'],
 ['E', 'F']]

I'm aware this is probably not 100% what you need, but it should be able to get you where you want

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145
  • @sundarnatarajサンダーナタラジ I know that as you can read in my last line. But an answer can be helpful and useful even if it doesn't provide the full code the OP wants – Tim Jun 03 '14 at 07:01
  • @sundarnatarajサンダーナタラジ no problem. Your answer works too so ill +1 you – Tim Jun 03 '14 at 07:03
  • @TimCastelijns Thanks to you too man, I'm reading, and this is helpful :) – Edek Jun 03 '14 at 07:04
  • @user3701822 good to hear ;-) don't hesitate to upvote useful answers, that goes for sundar's answer, too – Tim Jun 03 '14 at 07:06