在哪里或过滤列表 [英] where or filter on list
问题描述
有人能告诉我如何在列表中找到最近的零值
?
为此,我使用list comprenhension:
Hi,
Can anybody tell me how to to find the nearest value to zero in a list
?
To do that, i''m using list comprenhension :
> > foo = [ - 5,-1,2,3]#最接近零的值?
[foo中的值,如果math.fabs(value)== min([int(math.fabs(x) ))for f in foo])]
>>foo = [-5,-1,2,3] # nearest value to zero ?
[value for value in foo if math.fabs(value) == min([int(math.fabs(x)) for x in foo])]
[-1]
Something更简单?
如何将此功能扩展到任何给定值?
谢谢,
CH。
[-1]
Something simpler ?
How to extend this function to any given value ?
Thanks,
CH.
推荐答案
>>> foo = [ - 5,-1,2,3]#最接近零的值?
>>>foo = [-5,-1,2,3] # nearest value to zero ?
>>> [foo中的值,如果是math.fabs(值) )== min([int(math.fabs(x))for f in foo])]
>>>[value for value in foo if math.fabs(value) == min([int(math.fabs(x)) for x in foo])]
charles [-1 ]
charlesSomething更简单?
好吧,你可以使用abs()内置而不是math.fabs。另外,只需
一次计算min / abs:
minval = min([int(math.fabs(x))for f in foo])
[foo中的值,如果fabs(value)== minval]
另一种方法可能是按绝对值排序:
intermed = [(abs(v),v)for v in foo]
intermed.sort()
intermed [0] [1 ]
只给你一个可能接近零的值。
charles如何将这个函数扩展到任何给定的值?
只需从列表中的所有值中减去该值:
intermed = [(abs(vx),v)for f in foo]
intermed.sort()
intermed [0] [1]
Skip
charles[-1]
charlesSomething simpler ?
Well, you can just use the abs() builtin instead of math.fabs. Also, just
compute the min/abs once:
minval = min([int(math.fabs(x)) for x in foo])
[value for value in foo if fabs(value) == minval]
Another way might be to sort by absolute value:
intermed = [(abs(v), v) for v in foo]
intermed.sort()
intermed[0][1]
That only gives you one of possibly many values closest to zero.
charlesHow to extend this function to any given value ?
Just subtract that value from all values in the list:
intermed = [(abs(v-x), v) for v in foo]
intermed.sort()
intermed[0][1]
Skip
sk**@pobox.com 写道:
sk**@pobox.com wrote:
另一种方法可能是按绝对值排序:
intermed = [(abs(v),v)for f in foo]
intermed.sort( )
intermed [0] [1]
Another way might be to sort by absolute value:
intermed = [(abs(v), v) for v in foo]
intermed.sort()
intermed[0][1]
如果使用sorted(假设最近有足够的Python),它会稍微简单一点:
intermed = sorted(foo,key = abs)
print intermed [0]
排序列表当然是最好的方式,如果你想要的话找到一个不仅仅是一个价值的b $ b值,例如n最接近0.
对于最接近非零值的v,带有排序的版本变为:
intermed = sorted(foo) ,key = lambda x:abs(xv))
It is slightly simpler if you use sorted (assuming a recent enough Python):
intermed = sorted(foo, key=abs)
print intermed[0]
The sorted list is of course the best way if you want to find not just one
value but a group, e.g. the n nearest to 0.
For nearest to a non-zero value v the version with sorted becomes:
intermed = sorted(foo, key=lambda x:abs(x-v))
ch ************ @ gmail.com < ch ************ @ gmail.comwrote:
ch************@gmail.com <ch************@gmail.comwrote:
有人能告诉我如何在
列表中找到最接近的零值吗?
Can anybody tell me how to to find the nearest value to zero in a
list?
> foo = [-5,-1,2,3 ]#最接近零的值?
[foo中的值,如果math.fabs(value)== min([int(math.fabs(x))for f in foo])]
>foo = [-5,-1,2,3] # nearest value to zero ?
[value for value in foo if math.fabs(value) == min([int(math.fabs(x)) for x in foo])]
[-1]
[-1]
更简单?
Something simpler ?
也许是这样的:
mindist = min(地图(abs,foo))
filter(lambda x:abs(x)== mindist,a)[0]
只有烦恼:我们为每个值计算两次abs。我们可能通过使用reduce()和合适的函数来避免
...
Maybe something like this:
mindist = min(map(abs, foo))
filter(lambda x: abs(x) == mindist, a)[0]
Only annoyance: we compute abs twice for each value. We could probably
avoid that by using reduce() and a suitable function...
如何将此函数扩展到任何给定值?
How to extend this function to any given value ?
从foo中减去所需的值。
cu
Philipp
-
Philipp Pagel博士电话。 + 49-8161-71 2131
基因组定向生物信息学传真部。 + 49-8161-71 2186
慕尼黑技术大学
http://mips.gsf.de/staff/pagel
这篇关于在哪里或过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!