Jun-19-2017, 07:08 AM
(Jun-19-2017, 02:54 AM)Skaperen Wrote: that we the details of each type of test is away from the loop going through all the lines.Not sure I understand that...
it exit the loop after first match, so not all test are performed.
I also was thinking that if you put the check in a separate function, it can make the code more clear and contained, although maybe a bit longer
By the way - I see I have a mistake in the above code - the
else
is part of the if
statement, but it should be part of the for
loop (as was my intention). In the above code the count of skipped is wrong.datadict = {} options = [({4:'foo', 5:'bar', 9:'xyzzy', 10:'yzzyx', 14:'Skaperen'}, (1, 2, 3)), ({4:'bar', 5:'foo', 9:'yzzyx', 11:'xyzzy', 14:'Skaperen'}, (2, 1, 3))] for line in sys.stdin: tokens = line.rstrip().split() for opt, get_tokens in options: if all(tokens[k] == value for k, value in opt.items()): processed += 1 datadict[tokens[0]] = tuple((tokens[i] for i in get_tokens)) break else: skipped += 1