内置函数和和或? [英] builtin functions for and and or?

查看:64
本文介绍了内置函数和和或?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这么多:单行方式来做一个n-ary或者'或者''。


例如,


result = True

for x in L:

如果不是boolean_function(x):

result = False



I need this a lot: a one line way to do a n-ary and or ''or''.

e.g.,

result = True
for x in L:
if not boolean_function(x):
result = False

or

reduce(operator .__和__,[boolean_function(x)for x in L)
reduce(operator.__and__, [boolean_function(x) for x in L)




所以我通常只写一个小函数(L,boolean_function =

身份)或全部(...)。但我有点厌恶这样做所有的时间 - 它存在于Python库的任何地方吗?这对我来说似乎真的很贵。


第一种方式并不令人满意,因为它需要这么多行才能获得

基本上是一个原始的操作。第二种方式并不好,因为

它不具有可读性,许多读者不喜欢看到减少,即使它是b / b
是常见的成语那。我也不相信它是短路的。



So usually I just write a little function any( L, boolean_function =
identity ) or all( ... ). But I am kind of sick of doing that all the
time -- does it exist anywhere in the Python libraries? It seems really
common to me.

The first way isn''t satisfactory because it takes so many lines for what is
essentially one "primitive" operation. The second way isn''t great because
it is not as readable and many readers don''t like to see reduce, even if it
is a common idiom like that. Also I don''t believe it short circuits.

推荐答案

>所以通常我只是编写一个小函数(L,boolean_function =
> So usually I just write a little function any( L, boolean_function =
identity)或all(...)。但我有点厌倦了所有的时间 - 它存在于Python库的任何地方吗?这对我来说似乎很常见。


将东西放入您自己的模块并将其添加到您的python路径中。那么你

只需要写一次。

第一种方式并不令人满意,因为它需要这么多行才能获得

基本上是一个基元。操作。第二种方式并不好,因为它不具有可读性,许多读者不喜欢看到减少,即使
它是一种常见的习语。另外我不相信它会短路。
identity ) or all( ... ). But I am kind of sick of doing that all the
time -- does it exist anywhere in the Python libraries? It seems really
common to me.
Put things into your own module and add it to your python path. Then you
only have to write it once.
The first way isn''t satisfactory because it takes so many lines for what
is
essentially one "primitive" operation. The second way isn''t great because
it is not as readable and many readers don''t like to see reduce, even if
it
is a common idiom like that. Also I don''t believe it short circuits.




它不是,所以不是你的循环示例。在那里休息一下

结果是假的。


-

问候,


Diez B. Roggisch



It doesn''t but so doesn''t your loop example. Put a break in there once
Result is False.

--
Regards,

Diez B. Roggisch


我热烈推荐下载Peter Norvig的Python实用程序文件

http://aima.cs.berkeley.edu/python/utils.py )并把它放在你的
Python路径上。 (例如,在bash中,在.bashrc中添加一行如


导出PYTHONPATH =&/; / path /到/ utilities_directory"


utils.py文件定义了许多有用的功能,包括你想要的功能:


#def every(谓词,seq): />
#"""如果seq的每个元素都满足谓词,则为真。

#Ex:every(callable,[min,max])==> 1;每一个(可赎回,[分钟,3])

==> 0

#""

#for x in seq:

#if not predicate(x):return False

#return true



#def some(谓词,seq):

#"""如果seq的某个元素x满足谓词(x),则返回

谓词(x)。

#Ex:some(callable,[min,3])==> ; 1;一些(可赎回,[2,3])==> 0

#""

#for x in seq:

#px =谓词(x)

#if px:return px

#return False


Michael


-

Michael D. Hartl,Ph.D。

首席技术官
http://quarksports.com/

I warmly recommend downloading Peter Norvig''s Python utilities file
(http://aima.cs.berkeley.edu/python/utils.py) and putting it on your
Python path. (E.g., in bash, put a line like

export PYTHONPATH="/path/to/utilities_directory"

in your .bashrc file.) The utils.py file defines many useful
functions, including the ones you want:

# def every(predicate, seq):
# """True if every element of seq satisfies predicate.
# Ex: every(callable, [min, max]) ==> 1; every(callable, [min, 3])
==> 0
# """
# for x in seq:
# if not predicate(x): return False
# return True
#
# def some(predicate, seq):
# """If some element x of seq satisfies predicate(x), return
predicate(x).
# Ex: some(callable, [min, 3]) ==> 1; some(callable, [2, 3]) ==> 0
# """
# for x in seq:
# px = predicate(x)
# if px: return px
# return False

Michael

--
Michael D. Hartl, Ph.D.
Chief Technology Officer
http://quarksports.com/


Roose写道:
我需要这个很多:一个单行方式来做一个n-ary和/或''或''。
I need this a lot: a one line way to do a n-ary and or ''or''.




看起来有那些的itertools食谱,类似于什么

迈克尔刚发布。摘自此处:
http://docs.python。 org / lib / itertools-recipes.html

def all(seq,pred = bool):

"如果是pred,则返回True( x)对于ifilterfalse(pred,seq)中的elem中的每个元素,为true,对于元素为<<



返回False

返回True


def any(seq,pred = bool):

"如果对于

可迭代 
$ ifater中的元素
(pred,seq):

返回True

返回False

-

Brian Beck

一阶冒险家



Looks like there are itertools recipes for those, similar to what
Michael just posted. Taken from here:
http://docs.python.org/lib/itertools-recipes.html

def all(seq, pred=bool):
"Returns True if pred(x) is True for every element in the iterable"
for elem in ifilterfalse(pred, seq):
return False
return True

def any(seq, pred=bool):
"Returns True if pred(x) is True for at least one element in the
iterable"
for elem in ifilter(pred, seq):
return True
return False
--
Brian Beck
Adventurer of the First Order


这篇关于内置函数和和或?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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