ingo janssen wrote: > > > On 07/02/2019 11:08, Peter Otten wrote: >> replace the sequence of tests with dictionary lookups > > updated the gist a few times, now I could pre calculate the slices to be > taken per line, but will there be much gain compared to the copping from > the left side of the list?
Sorry, I don't understand the question. Looking at your code > if action == "%i": > lbl = function[action](content[action]) you really do not need the function[action] lookup here because you know that the result will always be f_number. Likewise you could bind content["%i"] to a name, labels, say, and then write if action == "%s": lbl = f_number(labels) which I find much more readable. A lookup table only makes sense if provides all necessary information. I tried to apply the idea to one of your gist versions: def set_lbl(items): global lbl lbl = f_number(items) def set_w(items): global v v = f_number(items) def set_f(items): global f f = f_number(items) def set_mx(items): global mx mx = mx_value_array(items, f) function = { "%i" : set_lbl, "%w" : set_w, "%s" : set_f, "%a" : set_mx, "%q" : f_vector, "%r" : f_value, "%p" : lambda items: f_vector_array(items, v), "%P" : lambda items: f_vector_array(items, v), "%o" : lambda items: f_value_array(items, v), "%m" : f_value, "%g" : f_number, "%E" : f_value, "%e" : lambda items: f_value_array(items, f), "%F" : f_value, "%A" : lambda items: f_value_array(items, mx + 1), "%f" : lambda items: f_value_array(items, f), "%t" : lambda items: f_nested_value_array(items, f), "%l" : lambda items: f_vector_array(items, f), "%n" : lambda items: f_value_array(items, f), "%v" : f_value, "%c" : f_vector, "%C" : f_vector } order = "%i %q %r %w %p %P %o %m %g %E %s %e %F %a %A %f %t %l %n %v %c %C" order = re.findall("%[a-z]",order,re.M|re.I) content = {} actions = [] for i in order: items = content[i] = [] actions.append(partial(function[i], items)) for points, line in enumerate(open("vorodat.txt.vol",'r'), 1): line = line.strip() line = line.split(" ") for action in actions: action() However, while the loop is rather clean now the rest of the code is sprinkled with implicit arguments and thus much worse than what you have. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor