for_some(),for_all()? [英] for_some(), for_all()?

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

问题描述

我正在尝试测试列表以查看某些或所有元素是否通过了测试。对于

示例,我可以使用reduce()来测试是否有任何元素为0.

lst = [1,2,0,3]
test = lambda x:x == 0
reduce(运算符.__或__,[test(x)for x in lst])



True


但我敢打赌reduce()不会利用短路逻辑。另外它是
创建另一个列表以进入reduce有点笨拙。有没有

相当于


for_some(test,lst)



for_all( test,lst)?

解决方案

使用列表理解,然后测试你想要的东西:


for_some


结果= [x代表x,如果<在此处插入您的测试>]


如果结果:



#至少有部分物品通过测试



否则:



#没有通过




for_all


result = [x for x在lst如果<在此插入您的测试>]


如果len(结果)== len(lst):



#所有项目都通过了考试



否则:



#至少有一个没人参加通过




我认为它更容易阅读和维护。


Larry Bates


" aurora" < AU ****** @ gmail.com>在消息中写道

news:op ************** @ news.cisco.com ...

我正在尝试测试列表以查看某些或所有元素是否通过测试。对于
示例,我可以使用reduce()来测试是否有任何元素为0.

lst = [1,2,0,3 ]
test = lambda x:x == 0
reduce(operator .__或__,[test(x)for x in lst])


True for_some(test,lst)

for_all(test,lst)?


aurora写道:

但我敢打赌reduce()不利用短路逻辑。另外
创建另一个列表以进入reduce有点笨拙。是否有一些相当于
for_some(test,lst)

for_all(test,lst)?



itertools文件中的任何()和all()都有一些食谱

做你想做的事:

http://www.python.org/doc/current/li...s -example.html

import itertools,timeit,operator
def any(seq) ,pred = bool):
...."如果pred(x)为真,则返回True如果

可迭代中的至少一个元素"

。 ...在imap中返回True(pred,seq)

.... def equals_0(x):return x == 0
.... items = range(500000)
timeit.Timer(reduce(运算符.__或___,[等于_ $(x)代表
项目中的x]),setup =" from __main__ import operator,equals_0,items")。timeit( 3)

4.1 040000915527344#timeit.Timer(" any(items,equals_0)",setup =" from __main__ import
itertools,any,equals_0,items")。timeit(3)

.... timeit.Timer(" any(items,equals_0)",setup =" from __main__ import
itertools,any,equals_0,items")。timeit(3)#sh短路

0.0 del items [0]
timeit.Timer(" any(items,equals_0)",setup =" from __main__ import



itertools,any,equals_0,items")。timeit(3)#no short-circuit,still more

1.6640000343322754

-

Michael Hoffman


aurora< au ****** @ gmail.com>写道:

我试图测试一个列表,看看是否有一些或所有元素通过测试。对于
示例,我可以使用reduce()来测试是否有任何元素为0.

lst = [1,2,0,3 ]
test = lambda x:x == 0
reduce(operator .__或__,[test(x)for x in lst])


True for_some(test,lst)

for_all(test,lst)?



最简单:


def for_some(test,lst):

for lst中的项目:

如果测试(项目):返回True


def for_all(test,lst):

for lst中的项目:

如果不测试(项目):返回False

返回True


你可以玩弄技巧,但你不能打败这些的简单性,并且

即使用最聪明的技巧你也不能超过他们的速度。并且

简洁是软件的绝佳品质。

Alex


I am trying to test a list to see if some or all elements pass a test. For
example I can use reduce() to test if any element is 0.

lst = [1,2,0,3]
test = lambda x: x == 0
reduce(operator.__or__,[test(x) for x in lst])


True

However I bet reduce() does not exploit the short circuit logic. Also it
is a little clumsy to create another list to pass into reduce. Is there
some equivalent of

for_some(test, lst)
or
for_all(test, lst)?

解决方案

Use list comprehension and then test for what you want:

for_some

result=[x for x in lst if <insert your test here>]

if result:
#
# At least some of the items passed the test
#
else:
#
# None passed
#

for_all

result=[x for x in lst if <insert your test here>]

if len(result) == len(lst):
#
# All items passed the test
#
else:
#
# At least one didn''t pass
#

I think it is easier to read and maintain.

Larry Bates

"aurora" <au******@gmail.com> wrote in message
news:op**************@news.cisco.com...

I am trying to test a list to see if some or all elements pass a test. For
example I can use reduce() to test if any element is 0.

lst = [1,2,0,3]
test = lambda x: x == 0
reduce(operator.__or__,[test(x) for x in lst])


True

However I bet reduce() does not exploit the short circuit logic. Also it
is a little clumsy to create another list to pass into reduce. Is there
some equivalent of

for_some(test, lst)
or
for_all(test, lst)?



aurora wrote:

However I bet reduce() does not exploit the short circuit logic. Also
it is a little clumsy to create another list to pass into reduce. Is
there some equivalent of

for_some(test, lst)
or
for_all(test, lst)?



There are some recipes for any() and all() in the itertools documents
that do what you want:

http://www.python.org/doc/current/li...s-example.html

import itertools, timeit, operator def any(seq, pred=bool): .... "Returns True if pred(x) is True at least one element in the
iterable"
.... return True in imap(pred, seq)
.... def equals_0(x): return x == 0 .... items = range(500000) timeit.Timer("reduce(operator.__or__, [equals_0(x) for x in items])", setup="from __main__ import operator, equals_0, items").timeit(3)
4.1040000915527344 #timeit.Timer("any(items, equals_0)", setup="from __main__ import itertools, any, equals_0, items").timeit(3)
.... timeit.Timer("any(items, equals_0)", setup="from __main__ import itertools, any, equals_0, items").timeit(3) # short-circuit
0.0 del items[0] timeit.Timer("any(items, equals_0)", setup="from __main__ import


itertools, any, equals_0, items").timeit(3) # no short-circuit, still faster
1.6640000343322754
--
Michael Hoffman


aurora <au******@gmail.com> wrote:

I am trying to test a list to see if some or all elements pass a test. For
example I can use reduce() to test if any element is 0.

lst = [1,2,0,3]
test = lambda x: x == 0
reduce(operator.__or__,[test(x) for x in lst])


True

However I bet reduce() does not exploit the short circuit logic. Also it
is a little clumsy to create another list to pass into reduce. Is there
some equivalent of

for_some(test, lst)
or
for_all(test, lst)?



Simplest:

def for_some(test, lst):
for item in lst:
if test(item): return True

def for_all(test, lst):
for item in lst:
if not test(item): return False
return True

You can play tricks, but you can''t beat the simplicity of these, and
even with the cleverest tricks you can''t beat their speed by much. And
simplicity is a great quality for software to have.
Alex


这篇关于for_some(),for_all()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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