activelearner
activelearner

Reputation: 7775

Substitute numpy functions with Python only

I have a python function that employs the numpy package. It uses numpy.sort and numpy.array functions as shown below:

def function(group):

    pre_data = np.sort(np.array(
        [c["data"] for c in group[1]], 
        dtype = np.float64
        ))

How can I re-write the sort and array functions using only Python in such a way that I no longer need the numpy package?

Upvotes: 0

Views: 175

Answers (3)

wim
wim

Reputation: 363133

Well, it's not strictly possible because the return type is an ndarray. If you don't mind to use a list instead, try this:

pre_data = sorted(float(c["data"]) for c in group[1])

Upvotes: 2

mgilson
mgilson

Reputation: 310069

It really depends on the code after this. pre_data will be a numpy.ndarray which means that it has array methods which will be really hard to replicate without numpy. If those methods are being called later in the code, you're going to have a hard time and I'd advise you to just bite the bullet and install numpy. It's popularity is a testament to it's usefulness...

However, if you really just want to sort a list of floats and put it into a sequence-like container:

def function(group):
     pre_data = sorted(float(c['data']) for c in group[1])

should do the trick.

Upvotes: 2

Eric
Eric

Reputation: 97641

That's not actually using any useful numpy functions anyway

def function(group):
    pre_data = sorted(float(c["data"]) for c in group[1])

Upvotes: 1

Related Questions