Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
...
like image 900
Edek Avatar asked Jun 09 '26 10:06

Edek


2 Answers

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']]
like image 121
sundar nataraj Avatar answered Jun 11 '26 23:06

sundar nataraj


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

like image 41
Tim Avatar answered Jun 11 '26 23:06

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!