Sorting containers in Python/numpy
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:
>>> import numpy as np
>>> n = 2
>>> u = np.array([v for v in range(19*n)])
>>> u = u.reshape(n,19)
>>> sorted(c, key=np.average)
[array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]),
array([19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37])]
精彩评论