Re: Newbi Q: Recursively reverse lists but NOT strings?

Discussion in 'Python' started by Kurt Smith, Oct 15, 2007.

  1. Kurt Smith

    Kurt Smith Guest

    On 10/15/07, Gary Herron <> wrote:
    > Dmitri O.Kondratiev wrote:
    > > Gary, thanks for lots of info!
    > > Python strings are not lists! I got it now. That's a pity, I need two
    > > different functions: one to reverse a list and one to reverse a string:

    > True, they are not both lists, but they *are* both sequences, with some
    > things in common. In particular xs[::-1] will reverse both types of
    > objects. And even if you roll you own reversal function, you don't need
    > two. One will do.
    >
    > Gary Herron
    >
    > >
    > > def reverseList(xs):
    > > if xs == []:
    > > return xs
    > > else:
    > > return (reverseList (xs[1:])) + [xs[0]]
    > >
    > > def reverseStr(str):
    > > if str == "":
    > > return str
    > > else:
    > > return (reverseStr (str[1:])) + str[0]
    > >
    > > Ok. Now regarding in-place reversal of a list:
    > >
    > > >>> l = [1,2,3]
    > > >>> l

    > > [1, 2, 3]
    > > >>> l.reverse()
    > > >>> l

    > > [3, 2, 1]
    > >
    > > That was, as I expected. Good.
    > >
    > > Then why this ? :
    > >
    > > >>> ls = [1,2,3].reverse()
    > > >>> ls
    > > >>>
    > > >>> print [1,2,3].reverse()

    > > None
    > > >>>

    > > I mean, why ls is empty after assignment?
    > >
    > > Also, I couldn't find in the Python docs what this form of slicing means:
    > > xs[::-1] ?
    > >
    > > It works for creating a reversed copy of either a string or a list,
    > > but what does '::-1' syntax means?


    mylist[::-1] is interpreted as mylist[slice(None,None,-1)], and the
    slice object has a method, 'indices' that computes the right endpoints
    for what you want to do.

    So:

    >>> myseq = (1,2,3)
    >>> myseq[::-1]

    (3, 2, 1)
    >>> myseq[slice(None,None,-1)]

    (3, 2, 1)
    >>> myseq[None:None:-1]

    (3, 2, 1)

    etc.



    > >
    > > Thanks,
    > >
    > > Dmitri O. Kondratiev
    > > <mailto:>
    > > http://www.geocities.com/dkondr
    > >
    > > On 10/15/07, *Gary Herron* <
    > > <mailto:>> wrote:
    > >
    > > Dmitri O.Kondratiev wrote:
    > > >
    > > > The function I wrote (below) reverses lists all right:
    > > >
    > > > def reverse(xs):
    > > > if xs == []:
    > > > return []
    > > > else:
    > > > return (reverse (xs[1:])) + [xs[0]]
    > > >
    > > >
    > > > >>> reverse ([1,2,3])
    > > > [3, 2, 1]
    > > > >>>
    > > >
    > > >
    > > > Yet when I try to reverse a string I get:
    > > >
    > > > >>> reverse ("abc")
    > > >
    > > > ...
    > > > ...
    > > > ...
    > > >
    > > > File "C:\wks\python-wks\reverse.py", line 5, in reverse
    > > >
    > > > return (reverse (xs[1:])) + [xs[0]]
    > > >
    > > > File "C:\wks\python-wks\reverse.py", line 5, in reverse
    > > >
    > > > return (reverse (xs[1:])) + [xs[0]]
    > > >
    > > > File "C:\wks\python-wks\reverse.py", line 2, in reverse
    > > >
    > > > if xs == []:
    > > >
    > > > RuntimeError: maximum recursion depth exceeded in cmp
    > > >
    > > > >>>
    > > >
    > > > What's wrong? Why recursion never stops?
    > > >

    > > If you are doing this as an python-learning exercise, then read
    > > on. If
    > > you are doing this reversal for real code, then try:
    > >
    > > xs.reverse() for in-place reversal of a list (but not a string), or
    > > result = xs[::-1] for creating a reversed copy of either a
    > > string or a
    > > list
    > >
    > >
    > > Your recursion stops when xs == [], but when you're stripping
    > > characters
    > > off a string, like 'abc', the remaining portion will be 'bc',
    > > then 'c',
    > > than '', but never [] so you 'll never stop.
    > >
    > > Try:
    > >
    > > if xs == []:
    > > return []
    > > elif xs == '':
    > > return ''
    > > else:
    > > ...
    > >
    > >
    > > Gary Herron
    > >
    > >
    > > >
    > > > Thanks,
    > > > Dima

    > >
    > >
    > >
    > >

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Kurt Smith, Oct 15, 2007
    #1
    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. Christoffer T

    Newbi q: show prog

    Christoffer T, Nov 26, 2003, in forum: Python
    Replies:
    9
    Views:
    351
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    444
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Victor B. Gonzalez
    Replies:
    5
    Views:
    312
    Bruno Desthuilliers
    Oct 17, 2007
  4. paul
    Replies:
    6
    Views:
    319
    Bruno Desthuilliers
    Oct 16, 2007
  5. Matt McCredie
    Replies:
    4
    Views:
    281
    Paul McGuire
    Oct 16, 2007
Loading...

Share This Page