Edek
Edek

Reputation: 33

Sorting a list in Python without repeat previous item

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)
...

Upvotes: 3

Views: 367

Answers (2)

sundar nataraj
sundar nataraj

Reputation: 8692

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']]

Upvotes: 4

Tim
Tim

Reputation: 43314

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

Upvotes: 2

Related Questions