Multiply a tuple by a constant

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay Davis

    Multiply a tuple by a constant

    I often need to multiply a tuple by a
    constant and the methods I use now are
    not very pretty. The idea is to get a
    new tuple that is some factor times the
    start tuple, like 'newtup = .5 * oldtup'.

    Is there a slick way to do this?
  • Peter Otten

    #2
    Re: Multiply a tuple by a constant

    Jay Davis wrote:
    [color=blue]
    > I often need to multiply a tuple by a
    > constant and the methods I use now are
    > not very pretty. The idea is to get a
    > new tuple that is some factor times the
    > start tuple, like 'newtup = .5 * oldtup'.
    >
    > Is there a slick way to do this?[/color]

    Not really slick, but it works:
    [color=blue][color=green][color=darkred]
    >>> tuple(map(5 .__mul__, (1,2,3)))[/color][/color][/color]
    (5, 10, 15)

    The space after the 5 is necessary. Alternatively, you can wrap the constant
    in brackets:
    [color=blue][color=green][color=darkred]
    >>> tuple(map((5)._ _mul__, (1,2,3)))[/color][/color][/color]
    (5, 10, 15)

    Peter

    Comment

    • Peter Otten

      #3
      Re: Multiply a tuple by a constant

      These days list comprehensions are preferred:
      [color=blue][color=green][color=darkred]
      >>> tuple([5*i for i in (1,2,3)])[/color][/color][/color]
      (5, 10, 15)


      Comment

      • Jeremy Sanders

        #4
        Re: Multiply a tuple by a constant

        On Fri, 13 Feb 2004 02:20:06 -0800, Jay Davis wrote:
        [color=blue]
        > I often need to multiply a tuple by a constant and the methods I use now
        > are not very pretty. The idea is to get a new tuple that is some factor
        > times the start tuple, like 'newtup = .5 * oldtup'.
        >
        > Is there a slick way to do this?[/color]

        I suggest looking at Numarray - a very convenient way of doing maths on
        sets of numbers. Of course if you don't do very much it's not worth doing
        this.

        The Space Telescope Science Institute helps humanity explore the universe with advanced space telescopes and ever-growing data archives.


        You can do things like:

        import numarray

        a = numarray.array( (1.,2.,5.,-46.) )
        a *= 0.5
        b = a + 3.4

        Jeremy

        Comment

        • Larry Bates

          #5
          Re: Multiply a tuple by a constant

          two ways:

          t=(1,2,3)
          newt=tuple(map( lambda x: x*0.5, t))

          Note: apparently the map function is slated for deprication
          (to be replaced by list comprehension)

          List comprehension:

          t=(1,2,3)
          newt=tuple([x*0.5 for x in t])

          -Larry Bates
          -----------------------------------------------------------
          "Jay Davis" <dj00302003@yah oo.com> wrote in message
          news:1d17eeb7.0 402130220.7258a 5bd@posting.goo gle.com...[color=blue]
          > I often need to multiply a tuple by a
          > constant and the methods I use now are
          > not very pretty. The idea is to get a
          > new tuple that is some factor times the
          > start tuple, like 'newtup = .5 * oldtup'.
          >
          > Is there a slick way to do this?[/color]


          Comment

          • Michele Simionato

            #6
            Re: Multiply a tuple by a constant

            dj00302003@yaho o.com (Jay Davis) wrote in message news:<1d17eeb7. 0402130220.7258 a5bd@posting.go ogle.com>...[color=blue]
            > I often need to multiply a tuple by a
            > constant and the methods I use now are
            > not very pretty. The idea is to get a
            > new tuple that is some factor times the
            > start tuple, like 'newtup = .5 * oldtup'.
            >
            > Is there a slick way to do this?[/color]

            You can always subclass tuple and redefine "*". Dunno if you would
            consider it slick enough.

            Michele

            Comment

            Working...