最小值列表 [英] min max of a list

查看:126
本文介绍了最小值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是列表。


值= [0,72,0,4,9,2,0,0,42,26,0,282,

23,0,101,0,0,0,0,0]


因为我们可以看到列表中有峰值。这是0,72 ,0是一个

组(三角形),峰值为72.then 0,4,9,2,0,0,峰值为

9和0,42,26, 0和42等等......

我想要的是每个bin(三角形)的左右边界索引。

列表可能尽可能大。所以一些herststic算法可以首先在列表中找到最大值并查找局部最大值和最小值以及

将其近似值组合在一起。然后找到下一个最大值并对二进制数组进行分组。所以

on。


这样我们就可以获得


[[0,2],[2, 7],[7,10],[10,13]]

(值列表中边界的索引)。所以第一组[0,2]

对应于值列表中的0,72,0等等...

希望我很清楚。

If this is the list.

values = [ 0, 72, 0, 4, 9, 2, 0, 0, 42, 26, 0, 282,
23, 0, 101, 0, 0, 0, 0, 0]

as we can see there are peaks in the list.that is 0,72,0 is a
group(triangle) with peak 72.then 0, 4, 9, 2, 0, 0 with peak
9 and 0, 42, 26, 0 with 42 and so on...
what I want is the left and right bound index of each bin(triangle).The
list could as big as possible.So some heurestic algorithm which could
first find max in the list and look for local maxima and minima and
group its adjcent bounds.Then find next max and group the bins and so
on.

so that we can get

[[0,2],[2,7],[7,10],[10,13]]
( indexes of the bounds in the values list). so first group [0,2]
correspond to 0,72,0 in the values list and so on...
Hope I am clear.

推荐答案

qu*****@gmail.com 写道:
如果这是列表。

values = [0,72,0,4,9,2,0,0,42,26 ,0,282,
23,0,101,0,0,0,0,0]

我们可以看到列表中有峰值。这是0,72, 0是一个
组(三角形),峰值为72.然后是0,4,9,2,0,0,峰值为9,0,42,26,0为42,依此类推......
我想要的是每个bin(三角形)的左右边界索引。
列表可以尽可能大。所以一些heuresstic算法可以首先在列表中找到max并且寻找当地的最大值和最小值并将它的相邻边界分组。然后找到下一个最大值并将分组分组等等。

这样我们就可以获得
< br [> [[0,2],[2,7],[7,10],[10 ,13]]
(值列表中的边界索引)。所以第一组[0,2]
对应于值列表中的0,72,0等等......
希望我很清楚。
If this is the list.

values = [ 0, 72, 0, 4, 9, 2, 0, 0, 42, 26, 0, 282,
23, 0, 101, 0, 0, 0, 0, 0]

as we can see there are peaks in the list.that is 0,72,0 is a
group(triangle) with peak 72.then 0, 4, 9, 2, 0, 0 with peak
9 and 0, 42, 26, 0 with 42 and so on...
what I want is the left and right bound index of each bin(triangle).The
list could as big as possible.So some heurestic algorithm which could
first find max in the list and look for local maxima and minima and
group its adjcent bounds.Then find next max and group the bins and so
on.

so that we can get

[[0,2],[2,7],[7,10],[10,13]]
( indexes of the bounds in the values list). so first group [0,2]
correspond to 0,72,0 in the values list and so on...
Hope I am clear.




不完全是你的输出,但希望你可以根据自己的需要定制它:


py> values = [0,72,0,4,9,2,0,0,42,26,0,282,23,0,101,0,0,

0,0,0 ]

py> def isnonzero((index,val)):

....返回val!= 0

....

py> import itertools

py>对于nonzero,vals in itertools.groupby(enumerate(values),isnonzero):

.... if nonzero:

.... vals = list(vals)

.... start = vals [0] [0] - 1

.... end = vals [-1] [0] + 1

....打印[开始,结束],值:,值[开头:结束+ 1]

....

[0,2]值:[0,72,0]

[2,6]值:[0,4,9,2,0]

[ 7,10]值:[0,42,26,0]

[10,13]值:[0,282,23,0]

[13, 15]值:[0,101,0]


请注意,实际工作由itertools.groupby完成。零项是

组合在一起并被忽略;非零条款汇集在一起​​。


STeVe



Not exactly your output, but hopefully you can tailor it to your needs:

py> values = [0, 72, 0, 4, 9, 2, 0, 0, 42, 26, 0, 282, 23, 0, 101, 0, 0,
0, 0, 0]
py> def isnonzero((index, val)):
.... return val != 0
....
py> import itertools
py> for nonzero, vals in itertools.groupby(enumerate(values), isnonzero):
.... if nonzero:
.... vals = list(vals)
.... start = vals[0][0] - 1
.... end = vals[-1][0] + 1
.... print [start, end], "values: ", values[start:end+1]
....
[0, 2] values: [0, 72, 0]
[2, 6] values: [0, 4, 9, 2, 0]
[7, 10] values: [0, 42, 26, 0]
[10, 13] values: [0, 282, 23, 0]
[13, 15] values: [0, 101, 0]

Note that the real work is done by itertools.groupby. Zero terms are
grouped together and ignored; non-zero terms are gathered together in lists.

STeVe


谢谢你。我的python版本没有找到groupby。我是使用python 2.3.2的
。有没有办法可以不使用groupby

Thanks for that. My version of python does''nt find "groupby". I am
using python 2.3.2. Is there a way I could do it with out using groupby


qu*****@gmail.com 写道:
谢谢你。我的python版本没有找到groupby。我使用python 2.3.2。有没有办法我可以使用groupby
Thanks for that. My version of python does''nt find "groupby". I am
using python 2.3.2. Is there a way I could do it with out using groupby



itertools.groupby在Python 2.4中。 docs [1]给出了一个相当于
的Python,所以如果由于某种原因你无法升级到当前的版本的Python b / b版本,你可以只复制groupby代码来自那里。


STeVe


[1] http://docs.python.org/lib/itertools-functions.html# l2h-1379



itertools.groupby is in Python 2.4. The docs[1] give a Python
equivalent, so if for some reason you can''t upgrade to the current
version of Python, you can just copy the groupby code from there.

STeVe

[1]http://docs.python.org/lib/itertools-functions.html#l2h-1379


这篇关于最小值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆