accessing elements in multi-dimensional sequences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rodrigo Daunaravicius

    accessing elements in multi-dimensional sequences

    Is there an elegant way to directly refer the 2nd dimension of a
    multi-dimensional sequence (like the nth character in a list of strings).
    An example would be deleting the newline in all the strings from a list
    obtained through readlines without recurring to 'for' loops.
    I would expect this to work:
    [color=blue][color=green][color=darkred]
    >>> d[/color][/color][/color]
    ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
    '0922210996\n'][color=blue][color=green][color=darkred]
    >>> del d[:][-1]
    >>> d[/color][/color][/color]
    ['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']

    But it doesn't, d remains unchanged.

    Another attempt produced what seemed to me like a counter-intuitive result
    [color=blue][color=green][color=darkred]
    >>> b=d[:][:-1]
    >>> b[/color][/color][/color]
    ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']


    Regards from a python newbie,
    Rodrigo Daunarovicius
  • Matteo Dell'Amico

    #2
    Re: accessing elements in multi-dimensional sequences

    Rodrigo Daunaravicius wrote:[color=blue]
    > Is there an elegant way to directly refer the 2nd dimension of a
    > multi-dimensional sequence (like the nth character in a list of strings).
    > An example would be deleting the newline in all the strings from a list
    > obtained through readlines without recurring to 'for' loops.
    > I would expect this to work:
    >
    >[color=green][color=darkred]
    >>>>d[/color][/color]
    >
    > ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
    > '0922210996\n']
    >[color=green][color=darkred]
    >>>>del d[:][-1]
    >>>>d[/color][/color]
    >
    > ['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']
    >
    > But it doesn't, d remains unchanged.
    >
    > Another attempt produced what seemed to me like a counter-intuitive result
    >
    >[color=green][color=darkred]
    >>>>b=d[:][:-1]
    >>>>b[/color][/color]
    >
    > ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
    >
    >
    > Regards from a python newbie,
    > Rodrigo Daunarovicius[/color]

    You can express that as [s[:-1] for s in d].
    Besides, strings are immutable: if s is a string, you can't do something
    like s[3] = 'a': though, you can create a new object and tell s to refer
    to it: s = s[:3] + 'a' + s[4:]

    If all you want to do is remove trailing whitespace, have also a look at
    the string strip, lstrip and rstrip methods.

    --
    Ciao,
    Matteo

    Comment

    • Peter Otten

      #3
      Re: accessing elements in multi-dimensional sequences

      Rodrigo Daunaravicius wrote:
      [color=blue]
      > Is there an elegant way to directly refer the 2nd dimension of a
      > multi-dimensional sequence (like the nth character in a list of strings).[/color]
      [list of strings example]

      While your example doesn't work, as Matteo already explained, the Numeric
      package provides an intuitive way to refer to the nth dimension of a
      matrix:
      [color=blue][color=green][color=darkred]
      >>> a = Numeric.reshape (range(9), (3,3))
      >>> a[/color][/color][/color]
      array([[0, 1, 2],
      [3, 4, 5],
      [6, 7, 8]])[color=blue][color=green][color=darkred]
      >>> a[:, :-1][/color][/color][/color]
      array([[0, 1],
      [3, 4],
      [6, 7]])

      In spite of its name, Numeric is not limited to numbers:
      [color=blue][color=green][color=darkred]
      >>> d = ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
      >>> a = Numeric.array(d )
      >>> a.tostring()[/color][/color][/color]
      '0891931243\n03 25443777\n09334 77028\n06996246 17\n'[color=blue][color=green][color=darkred]
      >>> a[:, :-1].tostring()[/color][/color][/color]
      '08919312430325 443777093347702 80699624617'

      Note that shorter strings are filled with spaces:
      [color=blue][color=green][color=darkred]
      >>> Numeric.array(["123\n", "4\n", "789\n"]).tostring()[/color][/color][/color]
      '123\n4\n 789\n'

      Of course this is overkill for your example use case.

      Peter

      Comment

      • Anton Vredegoor

        #4
        Re: accessing elements in multi-dimensional sequences

        Rodrigo Daunaravicius <rodelrod@hotma il.com> wrote:
        [color=blue]
        >Is there an elegant way to directly refer the 2nd dimension of a
        >multi-dimensional sequence (like the nth character in a list of strings).
        >An example would be deleting the newline in all the strings from a list
        >obtained through readlines without recurring to 'for' loops.[/color]

        d = ['0891931243\n', '0325443777\n', '0933477028\n',
        '0699624617\n', '0922210996\n']

        #swap rows and columns, with the side effect of turning
        #strings into lists, strings must be of equal length or
        #else some information will be lost:

        d1 = zip(*d)

        #remove a row (corresponds to a *column* in the old view)
        #this is an elementary operation now:

        del d1[-1]

        #swapping again restores the old row and column view:

        d1 = zip(*d1)

        #join the elements of the sublists in order to produce strings:

        d1 = map(''.join,d1)

        print d1

        #output is:
        #['0891931243', '0325443777', '0933477028', '0699624617',
        #'0922210996']


        While this way of coding enables one to *think* about the problem more
        efficiently it is not necessarily the most efficient algorithm for
        accomplishing this specific effect. If it's fast enough however, why
        not reduce ones mental computing cycles and let the computer do all
        the leg work?

        It might even ameliorate entropy problems later on in the evolution of
        the universe since the operations could probably be reversed more
        efficiently even if the computer moves more electrons through the
        silicon?

        Anton

        Comment

        • Duncan Booth

          #5
          Re: accessing elements in multi-dimensional sequences

          anton@vredegoor .doge.nl (Anton Vredegoor) wrote in
          news:40b72a41$0 $128$3a628fcd@r eader3.nntp.hcc net.nl:
          [color=blue]
          > d = ['0891931243\n', '0325443777\n', '0933477028\n',
          > '0699624617\n', '0922210996\n']
          >
          > #swap rows and columns, with the side effect of turning
          > #strings into lists, strings must be of equal length or
          > #else some information will be lost:
          >
          > d1 = zip(*d)
          >
          > #remove a row (corresponds to a *column* in the old view)
          > #this is an elementary operation now:
          >
          > del d1[-1]
          >
          > #swapping again restores the old row and column view:
          >
          > d1 = zip(*d1)
          >
          > #join the elements of the sublists in order to produce strings:
          >
          > d1 = map(''.join,d1)
          >
          > print d1
          >
          > #output is:
          > #['0891931243', '0325443777', '0933477028', '0699624617',
          > #'0922210996'][/color]

          This, of course, only works when the strings are all exactly the same
          length. If they are different lengths it truncates all the strings to the
          length of the shortest.

          Comment

          • Rodrigo Daunaravicius

            #6
            Re: accessing elements in multi-dimensional sequences

            Thank you all for you great tips! Matteo's list comprehension solution
            suits best my particular problem and I'll be using it.

            On Fri, 28 May 2004 10:34:15 GMT, Matteo Dell'Amico wrote:[color=blue]
            > You can express that as [s[:-1] for s in d].[/color]


            It was great to know about the Numeric package, Peter, it will be really
            useful for some of the matrix handling I'm going to need.
            Anton's solution was a good brain teaser. I couldn't find in the docs what
            is the meaning of the asterisk in zip(*d), though. Nothing to do with C
            pointers, I guess?


            On Fri, 28 May 2004 13:58:23 +0200, Anton Vredegoor wrote:
            [color=blue]
            > Rodrigo Daunaravicius <rodelrod@hotma il.com> wrote:
            >[color=green]
            >>Is there an elegant way to directly refer the 2nd dimension of a
            >>multi-dimensional sequence (like the nth character in a list of strings).
            >>An example would be deleting the newline in all the strings from a list
            >>obtained through readlines without recurring to 'for' loops.[/color]
            >
            > d = ['0891931243\n', '0325443777\n', '0933477028\n',
            > '0699624617\n', '0922210996\n']
            >
            > #swap rows and columns, with the side effect of turning
            > #strings into lists, strings must be of equal length or
            > #else some information will be lost:
            >
            > d1 = zip(*d)[/color]

            Thanks again,
            Rodrigo Daunaravicius

            Comment

            • Duncan Booth

              #7
              Re: accessing elements in multi-dimensional sequences

              Rodrigo Daunaravicius <rodelrod@hotma il.com> wrote in
              news:1epn0wa5bq b0r$.heo8w4umju xm$.dlg@40tude. net:
              [color=blue]
              > I couldn't find in the docs what
              > is the meaning of the asterisk in zip(*d), though. Nothing to do with C
              > pointers, I guess?[/color]

              Python Reference Manual, section 5.3.4 Calls:

              If the syntax "*expressio n" appears in the function call, "expression " must
              evaluate to a sequence. Elements from this sequence are treated as if they
              were additional positional arguments; if there are postional arguments
              x1,...,xN , and "expression " evaluates to a sequence y1,...,yM, this is
              equivalent to a call with M+N positional arguments x1,...,xN,y1,.. .,yM.

              Comment

              • Rodrigo Daunaravicius

                #8
                Re: accessing elements in multi-dimensional sequences

                On 28 May 2004 13:19:59 GMT, Duncan Booth wrote:
                [color=blue]
                > Rodrigo Daunaravicius <rodelrod@hotma il.com> wrote in
                > news:1epn0wa5bq b0r$.heo8w4umju xm$.dlg@40tude. net:
                >[color=green]
                >> I couldn't find in the docs what
                >> is the meaning of the asterisk in zip(*d), though. Nothing to do with C
                >> pointers, I guess?[/color]
                >
                > Python Reference Manual, section 5.3.4 Calls:
                >
                > If the syntax "*expressio n" appears in the function call, "expression " must
                > evaluate to a sequence. Elements from this sequence are treated as if they
                > were additional positional arguments; if there are postional arguments
                > x1,...,xN , and "expression " evaluates to a sequence y1,...,yM, this is
                > equivalent to a call with M+N positional arguments x1,...,xN,y1,.. .,yM.[/color]

                Got it. I was looking in the wrong places. '*' is not the easiest string to
                conduct a search on, if it's not referred to as an 'asterisk' somewhere on
                the text.

                Thanks, Duncan!
                Rodrigo Daunaravicius

                Comment

                Working...