Re: python list pattern matching?

2009-05-29 Thread Peter Otten
Terry Reedy wrote: a,b,*rest = list(range(10)) The list() call is superfluous. -- http://mail.python.org/mailman/listinfo/python-list

Re: python list pattern matching?

2009-05-29 Thread Chris Rebert
On Thu, May 28, 2009 at 3:57 PM, Terry Reedy tjre...@udel.edu wrote: guthrie wrote: I want to do a functional like pattern match to get teh first two elements, and then the rest of an array return value. For example, assume that perms(x) returns a list of values, and I want to do this:    

Re: python list pattern matching?

2009-05-29 Thread Terry Reedy
Peter Otten wrote: Terry Reedy wrote: a,b,*rest = list(range(10)) The list() call is superfluous. Agreed, even in Py3 when range() returns a range object rather than a list. -- http://mail.python.org/mailman/listinfo/python-list

python list pattern matching?

2009-05-28 Thread guthrie
I want to do a functional like pattern match to get teh first two elements, and then the rest of an array return value. For example, assume that perms(x) returns a list of values, and I want to do this: seq=perms(x) a = seq[0] b = seq[1] rest = seq[2:] Of course I can shorten to:

Re: python list pattern matching?

2009-05-28 Thread Terry Reedy
guthrie wrote: I want to do a functional like pattern match to get teh first two elements, and then the rest of an array return value. For example, assume that perms(x) returns a list of values, and I want to do this: seq=perms(x) a = seq[0] b = seq[1] rest = seq[2:] Of course

Re: python list pattern matching?

2009-05-28 Thread Mensanator
On May 28, 5:43 pm, guthrie guth...@mum.edu wrote: I want to do a functional like pattern match to get teh first two elements, and then the rest of an array return value. For example, assume that perms(x) returns a list of values, and I want to do this:     seq=perms(x)     a = seq[0]    

Re: python list pattern matching?

2009-05-28 Thread bearophileHUGS
Terry Reedy:   a,b,*rest = list(range(10))   a,b,rest (0, 1, [2, 3, 4, 5, 6, 7, 8, 9])   a,*rest,b = 'abcdefgh'   a,rest,b ('a', ['b', 'c', 'd', 'e', 'f', 'g'], 'h') For the next few years I generally suggest to specify the Python version too (if it's 2.x or 3.x). This is Python 3.x. Bye,

Re: python list pattern matching?

2009-05-28 Thread Steven D'Aprano
On Thu, 28 May 2009 18:57:42 -0400, Terry Reedy wrote: a,b,*rest = list(range(10)) That fails in Python 2.5 and 2.6. a,b,*rest = list(range(10)) File stdin, line 1 a,b,*rest = list(range(10)) ^ SyntaxError: invalid syntax -- Steven --

Re: python list pattern matching?

2009-05-28 Thread guthrie
Many thanks to all; perfect solution! -- http://mail.python.org/mailman/listinfo/python-list