473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

any() and all() shorthand

any( iterab ) and all( iterab )

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

What do you think?
Jan 7 '08 #1
7 1682
2008/1/7, ca********@gmail.com <ca********@gmail.com>:
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
Jan 7 '08 #2
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?
Jan 7 '08 #3
2008/1/7, ca********@gmail.com <ca********@gmail.com>:
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
Jan 7 '08 #4
On Jan 7, 1:29 pm, "Guilherme Polo" <ggp...@gmail.comwrote:
2008/1/7, castiro...@gmail.com <castiro...@gmail.com>:
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.

Jan 7 '08 #5
On Jan 7, 1:45 pm, castiro...@gmail.com wrote:
On Jan 7, 1:29 pm, "Guilherme Polo" <ggp...@gmail.comwrote:

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?
Jan 7 '08 #6
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


Jan 7 '08 #7
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?
Jan 8 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
7
by: Dave | last post by:
Occasionally, I've seen people write CSS code in shorthand for specifying font styles. I don't exactly remember how it goes, but sometimes I see things like this: SPAN {font: arial, helvetica...
9
by: Andy | last post by:
I'm a beginning student of CSS and the shorthand order for margins, etc. raised an eyebrow. The clockwise order is what bothers me: p.margin {margin: cm cm cm cm} I think the order should...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
3
by: Thief_ | last post by:
Is there a shorthand method for: Status = Status AND 1 I'm thinking that if you can do: StatusText &= "Test" and
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
1
by: Paul | last post by:
Hi all, I'm trying to find a declarative way to set server control attributes using the attribute="<%= SomeValue %>" syntax. This works fine for basic HTML controls, but server controls will...
42
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone...
10
by: =?iso-8859-1?B?dPZmZg==?= | last post by:
Usually I write something like ... if (x==1) { doSomething() } .... and that's how I see it in all the JS references I've found so far. But I have seen shorthand (?) for the same thing (?),...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.