I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:
fun (head : rest) = ...
So when I pass in a list, head
will be the first element, and rest
will be the trailing elements.
Likewise, in Python, I can automatically unpack tuples:
(var1, var2) = func_that_returns_a_tuple()
I want to do something similar with lists in Python. Right now, I have a function that returns a list, and a chunk of code that does the following:
ls = my_func()
(head, rest) = (ls[0], ls[1:])
I wondered if I could somehow do that in one line in Python, instead of two.
question from:
https://stackoverflow.com/questions/238102/pattern-matching-of-lists-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…