Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Is there a function to compute the local maximum in a list? while uing python

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

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.

max(MyList)

So in Python 2.6:

print max(MyList)

would print the largest value contained within the list.

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.

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?

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

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.

Start your 7-day free trial
Python

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

Can you give us some context?