I have this string:
[a [a b] [c e f] d]
and I want a list like this
lst[0] = "a"
lst[1] = "a b"
lst[2] = "c e f"
lst[3] = "d"
My current implementation that I don’t think is elegant/pythonic is two recursive functions (one splitting with ‘[‘
and the other with ‘]’ ) but I am sure it can be
done using list comprehensions or regular expressions (but I can’t figure out a sane way to do it).
Any ideas?
Actually this really isn’t a recursive data structure, note that
aanddare in separate lists. You’re just splitting the string over the bracket characters and getting rid of some white space.I’m sure somebody can find something cleaner, but if you want a one-liner something like the following should get you close: