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

2007-10-17 Thread Paddy
ot mode='private joke' story stargaming, I caught it first this time !-) intrusion pedant=TrueShouldn't that be s-o-r-r-y :-)/intrusion *ot -- http://mail.python.org/mailman/listinfo/python-list

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

2007-10-17 Thread Bruno Desthuilliers
Paddy a écrit : ot mode='private joke' story stargaming, I caught it first this time !-) intrusion pedant=TrueShouldn't that be s-o-r-r-y :-)/intrusion crying Oui :( /crying *ot -- http://mail.python.org/mailman/listinfo/python-list

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

2007-10-16 Thread Bruno Desthuilliers
OTanswering to Dmitri O.KondratievOT On Sunday 14 October 2007 5:06:19 pm 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]

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

2007-10-16 Thread Bruno Desthuilliers
Bruno Desthuilliers a écrit : OTanswering to Dmitri O.KondratievOT (snip) def reverse(xs): if xs: return xs else: return (reverse (xs[1:])) + [xs[0]] I meant: def reverse(xs): if not xs: (etc...) of course... ot mode='private joke' story stargaming, I caught it first

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

2007-10-16 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : (snip) def myreversed(sequence): if isinstance(sequence, basestring): return type(sequence)().join(reversed(sequence)) else: return type(sequence)(reversed(sequence)) (in fact, that's so simple I wonder why the built-in reversed() doesn't

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

2007-10-15 Thread Victor B. Gonzalez
On Sunday 14 October 2007 5:06:19 pm 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

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

2007-10-15 Thread Marc 'BlackJack' Rintsch
On Mon, 15 Oct 2007 02:11:27 -0400, Victor B. Gonzalez wrote: On Sunday 14 October 2007 5:06:19 pm 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]]

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

2007-10-15 Thread George Sakkis
On Oct 15, 2:30 am, Gary Herron [EMAIL PROTECTED] 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]

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

2007-10-15 Thread Gary Herron
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)

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

2007-10-15 Thread Dmitri O.Kondratiev
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: def reverseList(xs): if xs == []: return xs else: return (reverseList (xs[1:])) + [xs[0]] def

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

2007-10-15 Thread paul
Dmitri O.Kondratiev schrieb: 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: Not necessarily, you can handle both cases in one function: def reverse(xs): if xs in

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

2007-10-15 Thread Steven D'Aprano
On Mon, 15 Oct 2007 13:13:48 +0200, paul wrote: Dmitri O.Kondratiev schrieb: 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: Not necessarily, you can handle both cases

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

2007-10-15 Thread Francesco Guerrieri
On 10/15/07, Steven D'Aprano [EMAIL PROTECTED] wrote: ''.join(reversed(abc)) 'cba' list(reversed(range(3))) [2, 1, 0] It doesn't take much to make a more user-friendly version: def myreversed(sequence): if isinstance(sequence, basestring): return

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

2007-10-15 Thread Steven D'Aprano
On Mon, 15 Oct 2007 14:47:30 +0200, Francesco Guerrieri wrote: def myreversed(sequence): if isinstance(sequence, basestring): return type(sequence)().join(reversed(sequence)) else: return type(sequence)(reversed(sequence)) (in fact, that's so simple I wonder why the

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

2007-10-15 Thread Gary Herron
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

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

2007-10-15 Thread Francesco Guerrieri
On 10/15/07, Steven D'Aprano [EMAIL PROTECTED] wrote: On Mon, 15 Oct 2007 14:47:30 +0200, Francesco Guerrieri wrote: def myreversed(sequence): if isinstance(sequence, basestring): return type(sequence)().join(reversed(sequence)) else: return

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

2007-10-15 Thread Chris Mellon
On 10/15/07, Steven D'Aprano [EMAIL PROTECTED] wrote: On Mon, 15 Oct 2007 13:13:48 +0200, paul wrote: Dmitri O.Kondratiev schrieb: 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

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

2007-10-15 Thread Kurt Smith
On 10/15/07, Gary Herron [EMAIL PROTECTED] 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

Newbi Q: Recursively reverse lists but NOT strings?

2007-10-14 Thread Dmitri O.Kondratiev
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