Home > OS >  Multiple maximums in an interval
Multiple maximums in an interval

Time:10-16

I think what I'm trying to do should be theoretically simple but I just can't figure out what to do. BTW before I begin I should preface with that I'm only allowing myself to use scipy and numpy. (So more importantly I'm not allowed to use pandas)

I have this csv list with the column names 'Latitude','Longitude','Time','Quantity' (just a snippet the list is huge with a bunch of years) and what I'm trying to do is for each year find the biggest latitude. I'm having trouble separating the biggest element for each year. Here's my code for separating the columns

def read_file(x):
    # Reads the file and returns the 4 columns of datapoints as lists.
    data=np.genfromtxt(x, delimiter=',',usecols=range(4),dtype=None,unpack=True,names=True,encoding=None)
    date,time=np.genfromtxt(data['Time'],delimiter='T',dtype=None,unpack=True,encoding=None)
    year,month,day=np.genfromtxt(date,delimiter='-',dtype=None,unpack=True)
    return year,data['Latitude'] 
# I'm just returning the important things for now
# but the plan is to use all of the data for later tasks

The plan is to create another function that looks for whenever the current element is the same as the previous:

def max_per_year(x):
   year,lat=read_file(x)
   for i in range(len(year):
      if year[i]==year[i-1]:

and here's where I just can't figure out how to make it look through the previous is until it's different again.

CodePudding user response:

Lets think about what this code is doing for a second...

def max_per_year(x):
    latdic={}
    year,lat=read_file(x)
    for i in range(len(year)):
        if year[i]==year[i-1]:
            if float(lat[i]) >=float(lat[i-1]):
                latdic[str(year[i])]=lat[i]
        elif year[i]!=year[i-1]:
            latdic[str(year[i])]=lat[i]
    return latdic, max(lat)

Looking at this condition in particular: if float(lat[i]) >=float(lat[i-1]) - what this will do is it will evaluate each lat with the previous lat and replace the current max if it is greater.

So if I have an array [10, 1, 2] then this logic would return 2 as max number when what you need is 10. What you need to do is compare to the current max and not the previous index.

Also, you can drop the year comparisons entirely as it is just a key into a key value pair.

And think about this - what will [i-1] evaluate to when you are at the beginning of the array?

Try something like this:

def max_per_year(x):
    latdic={}
    year,lat=read_file(x)
    for i in range(len(year)):
        # at least one entry exist for year[i]
        if year[i] in latdic:
            # an entry exists, now check if current entry is less than lat[i]
            if latdic[year[i]] < lat[i]:
                latdic[year[i]]=lat[i]
        # no entry for year[i] key
        else:
            latdic[year[i]]=lat[i]
    return latdic
  • Related