My new position involves quite a bit of python coding. Before this I've only known java, so I keep trying to do things the java way not the python way. Python tends to give me this feeling that there are short simple ways of doing things, but that I can't figure them out.
For example, the following code takes two iterables and gives back a list containing tuples of indices on which the arrays agree.
def array_value_index_mapping(correct, comparison, compare_function= lambda x, y:x==y):
index_mapping = []
_list_comparor_recursive([], correct, [], comparison, index_mapping, compare_function)
return index_mapping
def _list_comparor_recursive(correct_index, correct_axes, comparison_index,\
comparison_axes, reduced_comparison_axes, compare_function):
if correct_axes.shape==() and comparison_axes.shape==():
if compare_function(correct_axes, comparison_axes):
if len(correct_index) == 1:
reduced_comparison_axes.append((correct_index[0], comparison_index[0]))
else:
reduced_comparison_axes.append((tuple(correct_index), tuple(comparison_index)))
elif not correct_axes.shape == ():
for i, row in enumerate(correct_axes):
correct_index.append(i)
_list_comparor_recursive(correct_index, row, comparison_index, comparison_axes, reduced_comparison_axes, compare_function)
correct_index.pop()
elif not comparison_axes.shape == ():
for i, row in enumerate(comparison_axes):
comparison_index.append(i)
_list_comparor_recursive(correct_index, correct_axes, comparison_index, row, reduced_comparison_axes, compare_function)
comparison_index.pop()
I use this code to do calculations like
result = [first_array[index_mapping[i][0]]-second_array[indexmapping[i][1] for i in range(len(index_mapping))]
It all looks too ugly to me.
Anyway, the point of this post is that such things as the above horror must result when ever a python newb tries his hand at things. I've tried looking for informative and useful lists of tricks that might help reduce this issue (and help me code in a way that doesn't cause brain damage). This result is this list...