python list pattern matching?

G

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

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

Terry Reedy

guthrie said:
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)
>>> 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')
 
C

Chris Rebert

guthrie said:
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)
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')

Note that this snazzy new syntax requires Python 3.0+

Cheers,
Chris
 
M

Mensanator

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!
 
B

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,
bearophile
 
S

Steven D'Aprano

That fails in Python 2.5 and 2.6.
File "<stdin>", line 1
a,b,*rest = list(range(10))
^
SyntaxError: invalid syntax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top