# local maximum in a list"python"

Is there a function to compute the local maximum in a list? while uing python
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Software EngineerCommented:
Python lists will grow to use available memory, so there is no "local maximum".

Can you give us some context?
Commented:
If you mean "How do I find the largest value contained within a list" Then the answer would be:

max(MyList)

So in Python 2.6:

print max(MyList)

would print the largest value contained within the list.
Author Commented:
well no i want to find a local maximum in a list of points like for example:

A = [1,4,9,2,10,4]

the local maximums in this list are 9 and 10 we define a local max point as :    pi-1 < pi and pi+1 < pi.

i want to know if a function already exists it may be faster than a for loop.
Commented:
Whilst I can't answer absolutely (I don't know every function/method supplied by Python), I would be surprised to find an in-built function for this as it is not something that would be required often enough.
Software EngineerCommented:
Given a list, you can use the max() routine to find the maximum value:

So:

A = [1,4,9,2,10,4]
print max( A )

If you want a specific number of maximum values, e.g., 2, then you could use something like this:

>>> A = [1,4,9,2,10,4]
>>> print max( A )
10
>>> A.sort()
>>> A.reverse()
>>> A[ :2 ]
[10, 9]
>>>

The syntax of [ :2 ] is called slicing, and says to take from the beginning of the sequence up to, but not including the value indexed by 2.  The first element is indexed by 0, so this returns a new list of 2 elements.

Does this help?
Commented:
No, this still wouldn't solve the original question.  I suspect that the original question asker can solve the problem (by examining the list in threes and checking for a local maximum that way), but was asking for an inbuilt function to solve his issue.
Commented:
Try the following snippet.  I guess that there is not any built-in function for that in standard modules.  The functionality is too special for that.

The function can be easily improved to get generator that is capable to consume any sequence and return the values on the fly.
``````def localMaxLst(lstIn):

if len(lstIn) == 0:
return []        # no results from empty list

lst = []             # init -- no results yet
m = lstIn[0]         # the first element as the maximum...
candidate = True     # ... candidate

for elem in lstIn[1:]:   # through elements from the second one
if elem > m:
m = elem         # better candidate
candidate = True
elif elem == m:      # if equal, local maximum was lost
candidate = False
elif elem < m:       # if lower then possible candidate to output
if candidate:
lst.append(m)
m = elem           # start again...
candidate = False  # being smaller it cannot be candidate

if candidate:        # if the peak at the very end
lst.append(m)
return lst

lst = [1, 4, 9, 2, 10, 4]
print lst, '-->', localMaxLst(lst)

lst = [1, 4, 9, 9, 2, 10, 4, 11]
print lst, '-->', localMaxLst(lst)

lst = [12, 1, 4, 9, 9, 2, 10, 4, 11]
print lst, '-->', localMaxLst(lst)
``````
Commented:
Here is the generator solution that can consume the values on the fly.
Commented:
... where it is? Who ate it? ;)

Here it is again...
``````def localMax(seq):
m = None             # not even initialized
candidate = False    # cannot be candidate

for elem in seq:     # loop through any iterable
if m is None:
m = elem         # the first is the initial value
candidate = True # and also is the candidate
elif elem > m:       # sharply greater
m = elem         # better candidate
candidate = True
elif elem == m:      # if equal, local maximum was lost
candidate = False
elif elem < m:       # if sharply lower then possible candidate to output
if candidate:
yield m
m = elem           # start again...
candidate = False  # being smaller it cannot be candidate

if candidate:        # if the peak at the very end
yield m          # the yield the last one

lst = [1, 4, 9, 2, 10, 4]
print lst, '-->', list(localMax(lst))

lst = [1, 4, 9, 9, 2, 10, 4, 11]
print lst, '-->', tuple(localMax(lst))

t = (12, 1, 4, 9, 9, 2, 10, 4, 11)
print t, '-->', list(localMax(t))

for e in localMax(t):
print e
``````

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
loool thank you Pepr! it's great.
thank you so much.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.