python list pattern matching?

Mensanator mensanator at aol.com
Fri May 29 01:05:21 CEST 2009


On May 28, 5:43 pm, guthrie <guth... at 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]
>     b = seq[1]
>     rest = seq[2:]
> Of course I can shorten to:
>     [a,b] = seq[0:2]
>     rest  = seq[2:]
>
> Can I find use some notation to do this?
>     [a,b,more] = perms(x)
> or conceptually:
>     [a,b,more..] = perms(x)

>>> perms = 'abcdefghijklmnopqrstuvwxyz'
>>> [a,b],more = perms[0:2],perms[2:]
>>> a
'a'
>>> b
'b'
>>> more
'cdefghijklmnopqrstuvwxyz'


>
> PROLOG & functional languages do list decomposition so nicely like
> this!




More information about the Python-list mailing list