accessing elements in multi-dimensional sequences

Discussion in 'Python' started by Rodrigo Daunaravicius, May 28, 2004.

  1. 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:

    >>> d

    ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
    '0922210996\n']
    >>> del d[:][-1]
    >>> d

    ['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']

    But it doesn't, d remains unchanged.

    Another attempt produced what seemed to me like a counter-intuitive result

    >>> b=d[:][:-1]
    >>> b

    ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']


    Regards from a python newbie,
    Rodrigo Daunarovicius
     
    Rodrigo Daunaravicius, May 28, 2004
    #1
    1. Advertising

  2. Rodrigo Daunaravicius wrote:
    > 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:
    >
    >
    >>>>d

    >
    > ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
    > '0922210996\n']
    >
    >>>>del d[:][-1]
    >>>>d

    >
    > ['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']
    >
    > But it doesn't, d remains unchanged.
    >
    > Another attempt produced what seemed to me like a counter-intuitive result
    >
    >
    >>>>b=d[:][:-1]
    >>>>b

    >
    > ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
    >
    >
    > Regards from a python newbie,
    > Rodrigo Daunarovicius


    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
     
    Matteo Dell'Amico, May 28, 2004
    #2
    1. Advertising

  3. Rodrigo Daunaravicius

    Peter Otten Guest

    Rodrigo Daunaravicius wrote:

    > 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).


    [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:

    >>> a = Numeric.reshape(range(9), (3,3))
    >>> a

    array([[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]])
    >>> a[:, :-1]

    array([[0, 1],
    [3, 4],
    [6, 7]])

    In spite of its name, Numeric is not limited to numbers:

    >>> d = ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
    >>> a = Numeric.array(d)
    >>> a.tostring()

    '0891931243\n0325443777\n0933477028\n0699624617\n'
    >>> a[:, :-1].tostring()

    '0891931243032544377709334770280699624617'

    Note that shorter strings are filled with spaces:

    >>> Numeric.array(["123\n", "4\n", "789\n"]).tostring()

    '123\n4\n 789\n'

    Of course this is overkill for your example use case.

    Peter
     
    Peter Otten, May 28, 2004
    #3
  4. Rodrigo Daunaravicius <> wrote:

    >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.


    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
     
    Anton Vredegoor, May 28, 2004
    #4
  5. Rodrigo Daunaravicius

    Duncan Booth Guest

    (Anton Vredegoor) wrote in
    news:40b72a41$0$128$:

    > 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']


    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.
     
    Duncan Booth, May 28, 2004
    #5
  6. 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:
    > You can express that as [s[:-1] for s in d].



    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:

    > Rodrigo Daunaravicius <> wrote:
    >
    >>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.

    >
    > 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)


    Thanks again,
    Rodrigo Daunaravicius
     
    Rodrigo Daunaravicius, May 28, 2004
    #6
  7. Rodrigo Daunaravicius

    Duncan Booth Guest

    Rodrigo Daunaravicius <> wrote in
    news:1epn0wa5bqb0r$.heo8w4umjuxm$:

    > 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?


    Python Reference Manual, section 5.3.4 Calls:

    If the syntax "*expression" 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.
     
    Duncan Booth, May 28, 2004
    #7
  8. On 28 May 2004 13:19:59 GMT, Duncan Booth wrote:

    > Rodrigo Daunaravicius <> wrote in
    > news:1epn0wa5bqb0r$.heo8w4umjuxm$:
    >
    >> 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?

    >
    > Python Reference Manual, section 5.3.4 Calls:
    >
    > If the syntax "*expression" 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.


    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
     
    Rodrigo Daunaravicius, May 28, 2004
    #8
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    455
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,958
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    689
    Vivek
    Aug 21, 2003
  4. Wirianto Djunaidi
    Replies:
    2
    Views:
    230
    Wirianto Djunaidi
    Apr 29, 2008
  5. Shreyas Satish
    Replies:
    2
    Views:
    129
    Aldric Giacomoni
    Apr 6, 2010
Loading...

Share This Page