#NEWLINE# def partialSums( item ) : #NEWLINE# #INDENT# partials = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for elem in item [ 1 ] : #NEWLINE# #INDENT# if ( elem [ 0 ] == 'B' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# A_row = elem [ 1 ] #NEWLINE# A_col = elem [ 2 ] #NEWLINE# A_val = elem [ 3 ] #NEWLINE# for elem in item [ 1 ] : #NEWLINE# #INDENT# if ( elem [ 0 ] == 'A' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# B_row = elem [ 1 ] #NEWLINE# B_col = elem [ 2 ] #NEWLINE# B_val = elem [ 3 ] #NEWLINE# if ( A_col == B_row ) : #NEWLINE# #INDENT# group = partials . setdefault ( ( B_col , B_col ) , [ ] ) #NEWLINE# group . append ( ( A_val * B_val ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# partial_sums = [ ( key , sum ( partials [ key ] ) ) for key in partials . keys ( ) ] #NEWLINE# return [ item for item in partial_sums if ( item [ 1 ] != 0 ) ]