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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • notnorwegian@yahoo.se

    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:
  • Scott David Daniels

    #2
    Re: test mult vars to same value, how to shorten expr?

    notnorwegian@ya hoo.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:
    How about:

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

    --Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: test mult vars to same value, how to shorten expr?

      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

      Comment

      • Peter Otten

        #4
        Re: test mult vars to same value, how to shorten expr?

        notnorwegian@ya hoo.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

        Comment

        • castironpi

          #5
          Re: test mult vars to same value, how to shorten expr?

          On May 20, 2:08 am, Peter Otten <__pete...@web. dewrote:
          notnorweg...@ya hoo.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.parti al( operator.eq, 5 )
          >>map( myequals, ( a, b, c, d ) )
          [True, True, True, True]
          >>all( _ )
          True

          Comment

          Working...