from re import compile, findall from locale import atoi uids = [] klists = [] def read_input(input): nedge = 0 check_uid =[0,0,0,0] klist = [] regex = compile('[^ \t\n]+') with open(input, 'r') as f: for line in f: indices = findall(regex, line) if len(indices) != 5 : print ('read input: len(indices)', len(indices)) exit nedge += 1 lid = indices[0:4] check_k = atoi(indices[4]) print ('nedge, check_k', nedge, check_k) if (lid == check_uid): #an existing uid klist.append(check_k) else: check_uid = lid uids.append(check_uid) # a new if len(klist): klists.append(klist) print ('nedge, klist', nedge, klist) klist.clear() klist.append(check_k) #end of if block #end of for block print(' No. of edges: ', nedge) print ('The last of the klists : nedge, klist', nedge, klist) klists.append(klist) # The last of the klists def generate_sudoku_data(): nleft = len(uids) nright = len(klists) print( ' No. of left vertices : ', nleft, ' No. of right vertices : ', nright) if nleft != nright: print ('generate_sudoku_data: nright', nright) exit for x in range(0, nleft, 1): klist = klists[x] print('x : ', x, ' klist : ', klist) def HK(input): read_input(input) generate_sudoku_data() if __name__ == "__main__": import sys HK(sys.argv[1])