Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > any() and all() shorthand

Reply
Thread Tools

any() and all() shorthand

 
 
castironpi@gmail.com
Guest
Posts: n/a
 
      01-07-2008
any( iterab ) and all( iterab )

as shorthand for reduce( operator.or_, iterab ) and
reduce( operator.and_, iterab ).

What do you think?
 
Reply With Quote
 
 
 
 
Guilherme Polo
Guest
Posts: n/a
 
      01-07-2008
2008/1/7, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)>:
> any( iterab ) and all( iterab )
>
> as shorthand for reduce( operator.or_, iterab ) and
> reduce( operator.and_, iterab ).
>
> What do you think?
> --
> http://mail.python.org/mailman/listinfo/python-list
>


You are too late, any and all are built-in into python 2.5

--
-- Guilherme H. Polo Goncalves
 
Reply With Quote
 
 
 
 
castironpi@gmail.com
Guest
Posts: n/a
 
      01-07-2008
> You are too late, any and all are built-in into python 2.5

Hi, excellent. Now how about something more generic, possibly:

[ x.y() for x or _next_ in c ]

where the context of _next_ is limited in complexity, and/or can only
occur in a generator?
 
Reply With Quote
 
Guilherme Polo
Guest
Posts: n/a
 
      01-07-2008
2008/1/7, (E-Mail Removed) <(E-Mail Removed)>:
> > You are too late, any and all are built-in into python 2.5

>
> Hi, excellent. Now how about something more generic, possibly:
>
> [ x.y() for x or _next_ in c ]
>
> where the context of _next_ is limited in complexity, and/or can only
> occur in a generator?


Would you care to explain what that syntax supposedly means ? By
_next_ you mean something like the next method in generators ? _next_
executes if x is false ? so whatever _next_ returns is named as x, so
you can call x.y() ? I really didn't get your new syntax inside that
list comprehension, neither its uses.

> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
-- Guilherme H. Polo Goncalves
 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      01-07-2008
On Jan 7, 1:29 pm, "Guilherme Polo" <(E-Mail Removed)> wrote:
> 2008/1/7, (E-Mail Removed) <(E-Mail Removed)>:
>
> > > You are too late, any and all are built-in into python 2.5

>
> > Hi, excellent. Now how about something more generic, possibly:

>
> > [ x.y() for x or _next_ in c ]

>
> > where the context of _next_ is limited in complexity, and/or can only
> > occur in a generator?

>
> Would you care to explain what that syntax supposedly means ? By
> _next_ you mean something like the next method in generators ? _next_
> executes if x is false ? so whatever _next_ returns is named as x, so
> you can call x.y() ? I really didn't get your new syntax inside that
> list comprehension, neither its uses.
>


The idea is a shorthand for reduce. Here, _next_ meant the next item
in the iterable c.

 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      01-07-2008
On Jan 7, 1:45 pm, (E-Mail Removed) wrote:
> On Jan 7, 1:29 pm, "Guilherme Polo" <(E-Mail Removed)> wrote:
>
> The idea is a shorthand for reduce. Here, _next_ meant the next item
> in the iterable c.


'Only' is another known quantifier in logic: 'all and only'. Any
(there exists) and all (for all) are too. 'Only' (and not others)
could be useful, from the theoretical standpoint. But where?
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      01-07-2008
> The idea is a shorthand for reduce. Here, _next_ meant the next item
> in the iterable c.


You mean like one of these:

def lookahead(iterator):
i = iter(iterator)
x = i.next()
for item in i:
yield x, item
x = item

def lookahead2(iterator, **kwarg):
i = iter(iterator)
if 'initial' in kwarg:
x = kwarg['initial']
else:
x = i.next()
for item in i:
yield x, item
x = item
if 'last' in kwarg:
yield x, kwarg['last']

print 'lookahead()'
for this, next in lookahead([1,2,3,4,5]):
print this, next

print 'lookahead2()'
for this, next in lookahead2([1,2,3,4,5]):
print this, next

print 'lookahead2(initial=42)'
for this, next in lookahead2([1,2,3,4,5], initial=42):
print this, next

print 'lookahead2(last=42)'
for this, next in lookahead2([1,2,3,4,5], last=42):
print this, next

print 'lookahead2(initial=3.14159, last=42)'
for this, next in lookahead2([1,2,3,4,5],
initial=3.14159, last=42):
print this, next


There are some alternate behaviors that can happen at the end
points, so depending on which behavior you want, the lookahead()
is cleanest, but doesn't allow you to handle edge cases. The
lookahead2() is a little more complex, but allows you to specify
a first item for pairing (so "next" touches every item in your
list) or a trailing element (so "this" touches every item).

-tkc






 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      01-08-2008
> print 'lookahead2(initial=3.14159, last=42)'
> for this, next in lookahead2([1,2,3,4,5],
> initial=3.14159, last=42):
> print this, next


No, actually. But my mistake.

[ a.b() or _previous_ for a in c ]

means

1 or 2 or 3 or 4 or 5
where c= [ 1, 2, 3, 4, 5].

The mistake: this was not a list comprehension; I wanted to reduce to
a single value.

It's equivalent to reduce( operator.or_, [ 1, 2, 3, 4, 5 ] ), but
disnecessitates lambdas for slightly more complex reductions. But the
example is out of stock. Do we have one?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shorthand operator for AND, NOT Archos Javascript 5 12-22-2011 09:54 AM
Shorthand for Property declaration in VB David W ASP .Net 0 01-17-2007 07:44 PM
foreach shorthand unspammable@gmail.com C++ 2 03-25-2005 10:38 PM
kiwi biz q_'n <== Teeline shorthand for question Quean Computer Support 2 12-23-2004 07:08 AM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments