English 中文(简体)
如何找到最高价值,次为最低值,然后是 p名单上的下一个最高值?
原标题:How to find the highest value, next lowest value, then the next highest value in a python list?

I want to find the highest and lowest values from a python list as mentioned below.

myList = [10,12,18,20,25,18,17,16,10,20,30,35,40,35,30,20,15]

我想找到。 本清单(25)中的第一个最高值是(10)项中第一个最高数值之后的最低数值,随后是先前最低数值(40)之后的下一个最高数值,因此,在清单结束之前,其最低和最高数值及其各自的指数值。

因此,我所尝试的很多东西是一份清单中价值最高和价值最低的,但我希望在名单结束和各自的指数价值之前分别找到最低和最高数值。

问题回答

您可尝试使用集体办法:

myList = [10,12,18,20,25,18,17,16,10,20,30,35,40,35,30,20,15]

from itertools import groupby

d = [ (myList[i],i) for _,(*_,i) in groupby(range(1,len(myList)),
                                    key=lambda i:myList[i-1]<myList[i]) ]

print(d)
[(25, 4), (10, 8), (40, 12), (15, 16)]

您还可以首先计算指数,并将数值分别计算:

myList = [10,12,18,20,25,18,17,16,10,20,30,35,40,35,30,20,15]

from itertools import groupby,accumulate

d         = (b>a for a,b in zip(myList,myList[1:]))
*indexes, = accumulate(len(g) for _,(*g,) in groupby(d)) 
values    = [myList[i] for i in indexes]

print(indexes)
print(values)

[4, 8, 12, 16]
[25, 10, 40, 15]

页: 1 in find find find

import itertools as it 

myList = [10,12,18,20,25,18,17,16,10,20,30,35,40,35,30,20,15]

for k,v in it.groupby(zip(myList, myList[1:]), lambda t: t[0]<t[1] ):
    print(list(v)[-1][-1])

印刷:

25
10
40
15

你们应当从清单中获取三个价值观并检查。

  • x1 < x2 > x3 to get local max
  • x1 > x2 < x3 to get local min

This is situation where you could use for-loop with range(len(...)) to work with index
to get three values myList[index-1], myList[index], myList[index+1] (instead of single value).

You should start at second item (index = 1) instead of first (index = 0)
because code uses index-1

And you should stop on len(myList)-1 instead of len(myList)
because code uses index+1

for i in range(1, len(myList)-1):
    print(myList[i-1], myList[i], myList[i+1])

and check

   is_max = (myList[i-1] < myList[i] > myList[i+1])
   is_min = (myList[i-1] > myList[i] < myList[i+1])

以后使用<代码>is_max和_is_min以显示或保持价值

    if is_max:
        # ... code ...
    elif is_min:
        # ... code ...
    else:
        # ... code ...

全面工作实例:

myList = [10,12,18,20,25,18,17,16,10,20,30,35,40,35,30,20,15]

for i in range(1, len(myList)-1):

    is_max = (myList[i-1] < myList[i] > myList[i+1])
    is_min = (myList[i-1] > myList[i] < myList[i+1])

    if is_max:
        print(f {i:3} | , myList[i-1], myList[i], myList[i+1],  max )
    elif is_min:
        print(f {i:3} | , myList[i-1], myList[i], myList[i+1],  min )
    else:
        print(f {i:3} | , myList[i-1], myList[i], myList[i+1])

结果:

  1 | 10 12 18
  2 | 12 18 20
  3 | 18 20 25
  4 | 20 25 18 max
  5 | 25 18 17
  6 | 18 17 16
  7 | 17 16 10
  8 | 16 10 20 min
  9 | 10 20 30
 10 | 20 30 35
 11 | 30 35 40
 12 | 35 40 35 max
 13 | 40 35 30
 14 | 35 30 20
 15 | 30 20 15

最后,您应使用<代码>>=,<=,而不是>,<





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签