Re: sequence multiplied by -1

Discussion in 'Python' started by Yingjie Lan, Sep 25, 2010.

  1. Yingjie Lan

    Yingjie Lan Guest

    --- On Sat, 9/25/10, Thomas Jollans <> wrote:
    > for every list l and integer n >= 0:
    >     len(l*n) == len(l)*n


    Well, this invariance is indeed broken under my proposal.
    But it is *already broken* in current python3k.
    However, the following invariance is maintained under
    my proposal:

    len(l*n) == len(l) * abs(n),

    which is also broken under current python3k.
    if you think len(..) as a mathematical norm,
    the above invariance makes perfect sense:

    || a * b || == ||a|| * |b|, b is real


    >
    > > Simply put, a sequence multiplied by -1 can give a

    > reversed sequence.
    >
    > For that, we have slicing. A negative step value produces a
    > reverse slice of
    > the list. You can't argue that this makes sense, can you
    >
    > >>> [1,2,3,4][::-1]

    > [4, 3, 2, 1]
    > >>> [1,2,3,4][::-2]

    > [4, 2]
    > >>>


    Having more than one way of doing things sometimes is good.
    Slicing is a little more complex, what if you want this:

    >>> ([1,2,3,4]*2)[::-1]

    [4, 3, 2, 1, 4, 3, 2, 1]

    under my new proposal, you simply do this:

    >>> [1,2,3,4]*-2

    [4, 3, 2, 1, 4, 3, 2, 1]

    Cheers,

    Yingjie
    Yingjie Lan, Sep 25, 2010
    #1
    1. Advertising

  2. Hi,

    On 2010-09-25 14:11, Yingjie Lan wrote:
    > Having more than one way of doing things sometimes is good.


    In my opinion this _isn't_ a situation where it's good. :)

    L[::-1]

    is only marginally longer than

    -1 * L

    I think this small gain doesn't justify "violating" this
    "Python Zen" rule (from `import this`):

    There should be one-- and preferably only one --obvious way to do it.

    One could argue that using L[::-1] isn't "obvious" but I
    believe the rule refers to being obvious to people who have
    used Python for a while. Besides that, multiplying a list
    with a negative value to give a reverse list isn't so
    intuitive either. :)

    On 2010-09-25 13:45, Thomas Jollans wrote:
    > Multiplying a list by a negative integer should produce a list of negative
    > length, which does not exist. IMHO, the only correct behaviour would be to
    > raise an exception, though one could argue that there are practical benefits
    > for the operation to succeed for any integer operand.


    I agree with raising an exception, probably a ValueError.

    Stefan
    Stefan Schwarzer, Sep 25, 2010
    #2
    1. Advertising

  3. On Sat, 25 Sep 2010 06:54:36 -0700 (PDT), Yingjie Lan
    <> declaimed the following in
    gmane.comp.python.general:

    >
    > >>> L*-3 #L reversed and repeated three times

    >

    At first glance, and if I didn't know that [] * # leads to
    # copies of the contents of []... I'd expect that syntax to multiply
    each element of "L" by a scalar -3...

    Which is probably what one would get from a numpy array (or would
    those require the form -3 * L ?)

    This is already an area where Python's list manipulations conflict
    with mathematical vector/scalar arithmetic; I'd not want to increase the
    degree of confusion by overloading the duplication operation with a
    reversal operation too...
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Sep 25, 2010
    #3
  4. Stefan Schwarzer a écrit :

    > One could argue that using L[::-1] isn't "obvious"


    It *is* obvious - once you've learned slicing. "obvious" doesn't mean
    you shouldn't bother reading the FineManual.
    Bruno Desthuilliers, Oct 3, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    545
    ma740988
    Dec 17, 2005
  2. klior

    2 Multiplied clock sync.

    klior, Jul 31, 2007, in forum: VHDL
    Replies:
    0
    Views:
    506
    klior
    Jul 31, 2007
  3. Yingjie Lan

    sequence multiplied by -1

    Yingjie Lan, Sep 25, 2010, in forum: Python
    Replies:
    2
    Views:
    263
    BartC
    Sep 26, 2010
  4. Thomas Jollans

    Re: sequence multiplied by -1

    Thomas Jollans, Sep 25, 2010, in forum: Python
    Replies:
    45
    Views:
    935
    Albert van der Horst
    Oct 6, 2010
  5. Yingjie Lan

    Re: sequence multiplied by -1

    Yingjie Lan, Sep 25, 2010, in forum: Python
    Replies:
    3
    Views:
    242
    Steven D'Aprano
    Sep 26, 2010
Loading...

Share This Page