Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

test mult vars to same value, how to shorten expr?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


notnorwegian at yahoo

May 19, 2008, 8:36 PM

Post #1 of 5 (125 views)
Permalink
test mult vars to same value, how to shorten expr?

if i want o test:
if a == 5 and b ==5 and c==5 ... z==5

is there some synctactic suagr for this?

rather than maiking one of my own i mean, something built-in like:
if a,b,c... z == 5:
--
http://mail.python.org/mailman/listinfo/python-list


Scott.Daniels at Acm

May 19, 2008, 9:12 PM

Post #2 of 5 (118 views)
Permalink
Re: test mult vars to same value, how to shorten expr? [In reply to]

notnorwegian [at] yahoo wrote:
> if i want o test:
> if a == 5 and b ==5 and c==5 ... z==5
>
> is there some synctactic suagr for this?
>
> rather than maiking one of my own i mean, something built-in like:
> if a,b,c... z == 5:
How about:

if 5 == a == b == c == ... z:
...

--Scott David Daniels
Scott.Daniels [at] Acm
--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

May 19, 2008, 11:41 PM

Post #3 of 5 (120 views)
Permalink
Re: test mult vars to same value, how to shorten expr? [In reply to]

On Mon, 19 May 2008 20:36:45 -0700, notnorwegian wrote:

> if i want o test:
> if a == 5 and b ==5 and c==5 ... z==5
>
> is there some synctactic suagr for this?
>
> rather than maiking one of my own i mean, something built-in like:
> if a,b,c... z == 5:

Since Python 2.5 there's `all()`:

In [81]: a, b, c, d = 5, 5, 5, 6

In [82]: all(x == 5 for x in (a, b, c))
Out[82]: True

In [83]: all(x == 5 for x in (a, b, c, d))
Out[83]: False

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

May 20, 2008, 12:08 AM

Post #4 of 5 (122 views)
Permalink
Re: test mult vars to same value, how to shorten expr? [In reply to]

notnorwegian [at] yahoo wrote:

> if i want o test:
> if a == 5 and b ==5 and c==5 ... z==5
>
> is there some synctactic suagr for this?
>
> rather than maiking one of my own i mean, something built-in like:
> if a,b,c... z == 5:

if all(x == 5 for x in a,b,c,...):
print "yep"


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


castironpi at gmail

May 20, 2008, 6:35 AM

Post #5 of 5 (96 views)
Permalink
Re: test mult vars to same value, how to shorten expr? [In reply to]

On May 20, 2:08 am, Peter Otten <__pete...@web.de> wrote:
> notnorweg...@yahoo.se wrote:
> >  if i want o test:
> > if a == 5 and b ==5 and c==5 ... z==5
>
> > is there some synctactic suagr for this?
>
> > rather than maiking one of my own i mean, something built-in like:
> > if a,b,c... z == 5:
>
> if all(x == 5 for x in a,b,c,...):
>    print "yep"
>
> Peter

>>> a,b,c,d=5,5,5,5
>>> import functools
>>> import operator
>>> myequals= functools.partial( operator.eq, 5 )
>>> map( myequals, ( a, b, c, d ) )
[True, True, True, True]
>>> all( _ )
True
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.