0

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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])

1 Comment

The return type is actually NoneType here, thanks to overzealous trimming by the OP!
1

That's not actually using any useful numpy functions anyway

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.