andmap and ormap
Diez B. Roggisch
deets at nospam.web.de
Tue Mar 14 07:31:39 EST 2006
> Does python have andmap and ormap:
>
> andmap((lambda t: boolean(t)),L)
>
> gives True if boolean(t) is True for all t in L and False otherwise?
> And
>
> ormap((lambda t: boolean(t)),L)
>
> gives True if boolean(t) is True for some t in L and False otherwise?
> One can use a list comprehension like
>
> [x for x in L if not(False in map((lambda t: boolean(t)),L))]
>
> as an example of selection by andmap, and
>
> [x for x in L if (True in map((lambda t: boolean(t)),L))]
>
> as an example of selection by ormap.
>
> How does one define andmap/ormap so its first argument is a boolean
> procedure or lambda?
>
> def andmap(b,L):
> if False in map(b,L): return False
> else: return True
>
> def ormap(b,L):
> if True in map(b,L): return True
> else: return False
import operator
reduce(operator.and_, [predcidate(o) for o in objects])
predicate can be a lambda, if it has to be.
Diez
More information about the Python-list
mailing list