I need to sort a special numpy array, in which blocks of size 19 constitute an element, using a user-defined function to determine the value of such a block.
The first attempt has been to wrap the array in a class and overload the [] operator:
class W:
def __init__(self, filename="nn.txt"):
self.nn = array([int(i) for i in open(filename, "r").readlines()[1:]])
self.size = self.nn.size / 19
def __getitem__(self, idx):
return self.nn[idx:idx+19]
def __len__(self):
return self.size
Using this structure I supply a comparison operator, which is passed to sorted():
def avg_cmp(x, y):
return int(average(x)) - int(average(y))
u = W("nnsmall.txt")
sorted(u, cmp=avg_cmp)
However, this approach is too slow.
Any tips?
have you tried
sorted(u, key=average)? this would only calculate the average of each column once.if the size of the array is always divisible by 19 without remainder: