consistency: extending arrays vs. multiplication ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Soeren Sonnenburg

    consistency: extending arrays vs. multiplication ?

    Hi all,

    Just having started with python, I feel that simple array operations '*'
    and '+' don't do multiplication/addition but instead extend/join an
    array:

    a=[1,2,3][color=blue][color=green][color=darkred]
    >>> b=[4,5,6]
    >>> a+b[/color][/color][/color]
    [1, 2, 3, 4, 5, 6]

    instead of what I would have expected:
    [5,7,9]

    or
    [color=blue][color=green][color=darkred]
    >>> 2*a[/color][/color][/color]
    [1, 2, 3, 1, 2, 3]

    Well it is consistent to strings but tolerating string-operations to be
    special is ok to me as "a" + "b" -> 'ab' :)
    Why not make it another function like a.stretch() or a.expand() and
    a.extend() is there doing the same anyway and is more readable...

    Putting this in a larger view:
    Ufuncs are very reasonable sin(a), etc ... all that won't work because
    of that '+','*' syntax. Ok I can use numarray for that, but seeing its
    PEP and a possible inclusion into python at some point that
    inconsistency is giving me quite some headache...

    Will that change in the future ? Or is this 100*[0] syntax put into
    stone for all ages ?

    Best,
    Soeren.

    PS: As I am very new to python please forgive/correct me!

  • Marc 'BlackJack' Rintsch

    #2
    Re: consistency: extending arrays vs. multiplication ?

    In <mailman.2115.1 122136234.10512 .python-list@python.org >, Soeren
    Sonnenburg wrote:
    [color=blue]
    > Just having started with python, I feel that simple array operations '*'
    > and '+' don't do multiplication/addition but instead extend/join an
    > array:
    >
    > a=[1,2,3][color=green][color=darkred]
    >>>> b=[4,5,6]
    >>>> a+b[/color][/color]
    > [1, 2, 3, 4, 5, 6][/color]

    Both operate on the lists themselves and not on their contents. Quite
    consistent if you ask me.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Steven D'Aprano

      #3
      Re: consistency: extending arrays vs. multiplication ?

      On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
      [color=blue]
      > Hi all,
      >
      > Just having started with python, I feel that simple array operations '*'
      > and '+' don't do multiplication/addition but instead extend/join an
      > array:[/color]

      * and + are not array operations, they are list operations.

      Lists in Python can contain anything, not just numeric values.

      Python doesn't have built-in mathematical arrays, otherwise known as
      matrices. There are modules that do that, but I haven't used them. Google
      on Numeric Python.



      --
      Steven.

      Comment

      • Dan Bishop

        #4
        Re: consistency: extending arrays vs. multiplication ?

        Soeren Sonnenburg wrote:[color=blue]
        > Hi all,
        >
        > Just having started with python, I feel that simple array operations '*'
        > and '+' don't do multiplication/addition but instead extend/join an
        > array:
        >
        > a=[1,2,3][color=green][color=darkred]
        > >>> b=[4,5,6]
        > >>> a+b[/color][/color]
        > [1, 2, 3, 4, 5, 6]
        >
        > instead of what I would have expected:
        > [5,7,9][/color]

        To get what you expected, use

        [x + y for (x, y) in zip(a, b)]

        Comment

        • Soeren Sonnenburg

          #5
          Re: consistency: extending arrays vs. multiplication ?

          On Sat, 2005-07-23 at 23:35 +0200, Marc 'BlackJack' Rintsch wrote:[color=blue]
          > In <mailman.2115.1 122136234.10512 .python-list@python.org >, Soeren
          > Sonnenburg wrote:
          >[color=green]
          > > Just having started with python, I feel that simple array operations '*'
          > > and '+' don't do multiplication/addition but instead extend/join an
          > > array:
          > >
          > > a=[1,2,3][color=darkred]
          > >>>> b=[4,5,6]
          > >>>> a+b[/color]
          > > [1, 2, 3, 4, 5, 6][/color]
          >
          > Both operate on the lists themselves and not on their contents. Quite
          > consistent if you ask me.[/color]

          But why ?? Why not have them operate on content, like is done on
          *arrays ?

          Soeren

          Comment

          • Soeren Sonnenburg

            #6
            Re: consistency: extending arrays vs. multiplication ?

            On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:[color=blue]
            > On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
            >[color=green]
            > > Hi all,
            > >
            > > Just having started with python, I feel that simple array operations '*'
            > > and '+' don't do multiplication/addition but instead extend/join an
            > > array:[/color]
            >
            > * and + are not array operations, they are list operations.
            >
            > Lists in Python can contain anything, not just numeric values.[/color]

            That seems to be *the point*. Although list(a) + list(b) could create a
            list [ a[0]+b[0], ...] and bail out if for elements '+' is not
            defined...
            [color=blue]
            > Python doesn't have built-in mathematical arrays, otherwise known as
            > matrices. There are modules that do that, but I haven't used them. Google
            > on Numeric Python.[/color]

            Well I am aware of that but I don't understand the reasons of having
            both lists (which are infect arrays) and *arrays ? *I* would rather drop
            '+' and '*' to work like they do in *array ...

            Soeren

            Comment

            • Soeren Sonnenburg

              #7
              Re: consistency: extending arrays vs. multiplication ?

              On Sat, 2005-07-23 at 20:25 -0700, Dan Bishop wrote:[color=blue]
              > Soeren Sonnenburg wrote:[color=green]
              > > Hi all,
              > >
              > > Just having started with python, I feel that simple array operations '*'
              > > and '+' don't do multiplication/addition but instead extend/join an
              > > array:
              > >
              > > a=[1,2,3][color=darkred]
              > > >>> b=[4,5,6]
              > > >>> a+b[/color]
              > > [1, 2, 3, 4, 5, 6]
              > >
              > > instead of what I would have expected:
              > > [5,7,9][/color]
              >
              > To get what you expected, use
              >
              > [x + y for (x, y) in zip(a, b)][/color]

              Thanks for this suggestion, however I am interested in understanding the
              design decision here... I could aswell just use numarray and get the
              wanted a+b by:

              from numarray import *
              a=array([1,2,3])
              b=array([1,2,3])
              a+b
              array([2, 4, 6])

              Soeren

              Comment

              • Robert Kern

                #8
                Re: consistency: extending arrays vs. multiplication ?

                Soeren Sonnenburg wrote:[color=blue]
                > On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:
                >[color=green]
                >>On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
                >>[color=darkred]
                >>>Hi all,
                >>>
                >>>Just having started with python, I feel that simple array operations '*'
                >>>and '+' don't do multiplication/addition but instead extend/join an
                >>>array:[/color]
                >>
                >>* and + are not array operations, they are list operations.
                >>
                >>Lists in Python can contain anything, not just numeric values.[/color]
                >
                > That seems to be *the point*.[/color]

                Whose point? If you mean that you want to be able to use arbitrary
                objects in an array, then look in numarray.object s for an array type
                that handles arbitrary Python objects.
                [color=blue]
                > Although list(a) + list(b) could create a
                > list [ a[0]+b[0], ...] and bail out if for elements '+' is not
                > defined...[/color]

                Unlike the current situation, where a+b always works consistently
                despite the contents, despite how long the lists are.
                [color=blue][color=green]
                >>Python doesn't have built-in mathematical arrays, otherwise known as
                >>matrices. There are modules that do that, but I haven't used them. Google
                >>on Numeric Python.[/color]
                >
                > Well I am aware of that but I don't understand the reasons of having
                > both lists (which are infect arrays)[/color]

                They "are in [fact] arrays" only in the sense that they are containers
                of objects with a contiguous layout in memory. That doesn't imply either
                set of semantics for + and * operators.
                [color=blue]
                > and *arrays ?[/color]

                They're good at different things. Arrays like Numeric/numarray are
                harder to implement than the builtin lists.
                [color=blue]
                > *I* would rather drop
                > '+' and '*' to work like they do in *array ...[/color]

                Tough. It's 14 years or so too late to make that change.

                --
                Robert Kern
                rkern@ucsd.edu

                "In the fields of hell where the grass grows high
                Are the graves of dreams allowed to die."
                -- Richard Harter

                Comment

                • Carl Banks

                  #9
                  Re: consistency: extending arrays vs. multiplication ?



                  Soeren Sonnenburg wrote:[color=blue]
                  > On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:[color=green]
                  > > On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
                  > >[color=darkred]
                  > > > Hi all,
                  > > >
                  > > > Just having started with python, I feel that simple array operations '*'
                  > > > and '+' don't do multiplication/addition but instead extend/join an
                  > > > array:[/color]
                  > >
                  > > * and + are not array operations, they are list operations.
                  > >
                  > > Lists in Python can contain anything, not just numeric values.[/color]
                  >
                  > That seems to be *the point*. Although list(a) + list(b) could create a
                  > list [ a[0]+b[0], ...] and bail out if for elements '+' is not
                  > defined...
                  >[color=green]
                  > > Python doesn't have built-in mathematical arrays, otherwise known as
                  > > matrices. There are modules that do that, but I haven't used them. Google
                  > > on Numeric Python.[/color]
                  >
                  > Well I am aware of that but I don't understand the reasons of having
                  > both lists (which are infect arrays) and *arrays ? *I* would rather drop
                  > '+' and '*' to work like they do in *array ...[/color]


                  The number of programmers who do operations on mathematical arrays is
                  pretty small. The number of programmers who need to do things like
                  concatenate lists is much larger. Thus, the decision was made to use
                  the valuable operator for the more common thing.

                  Truth be told, I rarely use + on lists (I tend to use list.extend
                  mostly), and if + had instead been used for element-by-element
                  operations, I don't think it would have affected the overall quality of
                  Python too much. But, as it's been said, it's a little late to change
                  it now.


                  --
                  CARL BANKS

                  Comment

                  • Christopher Subich

                    #10
                    Re: consistency: extending arrays vs. multiplication ?

                    Soeren Sonnenburg wrote:[color=blue]
                    > On Sat, 2005-07-23 at 23:35 +0200, Marc 'BlackJack' Rintsch wrote:[color=green]
                    >>Both operate on the lists themselves and not on their contents. Quite
                    >>consistent if you ask me.[/color][/color]
                    [color=blue]
                    > But why ?? Why not have them operate on content, like is done on
                    > *arrays ?[/color]

                    Because they're lists, not arrays. What do you propose that the
                    following do:

                    [1,2,3] + [4,5,6]
                    [1,2] + [3,4,5]
                    [1,2] + [{3:4,5:6}]
                    dict_var_1.keys () + dict_var_2.keys ()
                    [g(3) for g in [f1 f2 f3] + [f4 f5 f6]]

                    I point out that the idiom is <list> + <list>, not <numbers> +
                    <numbers>. Operations on lists must deal with them as lists, not lists
                    of any specific type.

                    Comment

                    • Soeren Sonnenburg

                      #11
                      Re: consistency: extending arrays vs. multiplication ?

                      On Sun, 2005-07-24 at 11:50 -0700, Robert Kern wrote:[color=blue]
                      > Soeren Sonnenburg wrote:[color=green]
                      > > On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:
                      > >[color=darkred]
                      > >>On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:[/color][/color][/color]
                      [...][color=blue][color=green][color=darkred]
                      > >>Lists in Python can contain anything, not just numeric values.[/color]
                      > >
                      > > That seems to be *the point*.[/color]
                      >
                      > Whose point? If you mean that you want to be able to use arbitrary
                      > objects in an array, then look in numarray.object s for an array type
                      > that handles arbitrary Python objects.[/color]

                      Well, one cannot efficiently deal with these 'list-arrays' as they can
                      contain different data types (typechecking necessary; atlas etc won't
                      work).

                      [...][color=blue][color=green]
                      > > *I* would rather drop
                      > > '+' and '*' to work like they do in *array ...[/color]
                      >
                      > Tough. It's 14 years or so too late to make that change.[/color]

                      Ok got it.

                      A seperate array type which can only contain objects of the same type
                      simply makes sense.

                      Soeren

                      Comment

                      • Soeren Sonnenburg

                        #12
                        Thanks (was Re: consistency: extending arrays vs. multiplication ?)

                        Thanks a lot for all the answers!!

                        Soeren

                        Comment

                        Working...