hexadec0079
hexadec0079

Reputation: 323

Array sorting issue in Python

I am attempting to sort data (x) and find how many times each record is greater than any previous record. I have it starting with the the second record (index 1) in my range and then comparing to the max of values from index 0 up to i. I am having a hard time and have tried breaking it down to steps and am unsure of how it is failing? Can anyone lend any insights? Many thanks, hopefully I explained it properly.

def greater(x):
g=0
for i in range(len(x[1:])):
    if x[i] > np.max(x[:i]):
        g = g + 1            
return g

Expected result:

x=[0,1,1,5,4] g should = 2, at record index 1,3

Upvotes: 1

Views: 73

Answers (3)

William McBrine
William McBrine

Reputation: 2266

As lejlot said, it's not necessary to keep recalculating the maximum of all the previous list items each time -- just record the running maximum as you go through the list once. This also means that you don't need to keep reslicing the list, so you don't have to keep track of your current position in the list -- just the current item -- and therefore, you don't need range() or len(), either. This leads to a very simple implementation:

def greater(x):
    g = 0
    newmax = x[0]
    for i in x[1:]:
        if i > newmax:
            g += 1
            newmax = i
    return g

Upvotes: 0

Jaime
Jaime

Reputation: 67447

You can vectorize this with numpy as follows:

>>> x = [0, 1, 1, 5, 4]
>>> np.count_nonzero(x[1:] > np.maximum.accumulate(x)[:-1])
2

To understand what's going on:

>>> np.maximum.accumulate(x)
array([0, 1, 1, 5, 5])
>>> x[1:] > np.maximum.accumulate(x)[:-1]
array([ True, False,  True, False], dtype=bool)

You can get the indices of the positions where g is increased with:

>>> np.nonzero(x[1:] > np.maximum.accumulate(x)[:-1])[0] + 1
array([1, 3], dtype=int64)

Upvotes: 1

lejlot
lejlot

Reputation: 66835

You are performing your maing loop wrong

It should be:

for i in range(1,len(x)):

as you want to loop through values from 1 to the length of x, while your code loops from 0 to length of x minus 1

btw. it has nothing to do with "array comprehension"

It would be also more efficient to store the current max value instead of performing np.max(x[:i]) (which is linear).

Upvotes: 1

Related Questions