20 Dec 2015 python

Python: Squashing 'duplicate' pairs together

A B (A is the same as B) B C (B is the same as C) C D ... E F (E is the same as F) F G ...

(A, B, C, D) (E, F, G)

pairs = [ ( "A" , "B" ), ( "B" , "C" ), ( "C" , "D" ), ( "E" , "F" ), ( "F" , "G" ) ]

def find_matching_index (pair, dups): return [index for index, dup in enumerate(dups) if pair[ 0 ] in dup or pair[ 1 ] in dup] print find_matching_index(( "A" , "B" ), [set([ "D" , "E" ])]) [] print find_matching_index(( "B" , "C" ), [set([ "A" , "B" ])]) [ 0 ] print find_matching_index(( "B" , "C" ), [set([ "A" , "B" ]), set([ "C" , "D" ])]) [ 0 , 1 ]

def extract_groups (items): dups = [] for pair in items: matching_index = find_matching_index(pair, dups) if len(matching_index) == 0 : dups . append(set([pair[ 0 ], pair[ 1 ]])) elif len(matching_index) == 1 : index = matching_index[ 0 ] matching_dup = dups[index] dups . pop(index) dups . append(matching_dup . union([pair[ 0 ], pair[ 1 ]])) else : index1, index2 = matching_index dup1 = dups[index1] dup2 = dups[index2] dups . pop(index1) dups . pop(index2 - 1 ) # the index decrements since we removed one entry on the previous line dups . append(dup1 . union(dup2)) return dups

test_cases = [ [ ( "A" , "B" ), ( "B" , "C" ), ( "C" , "D" ), ( "E" , "F" ), ( "F" , "G" ) ], [ ( "A" , "B" ), ( "B" , "C" ), ( "C" , "D" ), ( "E" , "F" ), ( "F" , "G" ), ( "G" , "A" ), ( "G" , "Z" ), ( "B" , "D" ) ], [ ( "A" , "B" ), ( "B" , "C" ), ( "C" , "E" ), ( "E" , "A" ) ], [ ( "A" , "B" ), ( "C" , "D" ), ( "F" , "G" ), ( "H" , "I" ), ( "J" , "A" ) ] ] for test_case in test_cases: print extract_groups(test_case) [set([ 'A' , 'C' , 'B' , 'D' ]), set([ 'E' , 'G' , 'F' ])] [set([ 'A' , 'C' , 'B' , 'E' , 'D' , 'G' , 'F' , 'Z' ])] [set([ 'A' , 'C' , 'B' , 'E' ])] [set([ 'C' , 'D' ]), set([ 'G' , 'F' ]), set([ 'I' , 'H' ]), set([ 'A' , 'J' , 'B' ])]

Email







Tumblr

Reddit



Pinterest

