Pythonのジェネレータを再帰的に呼ぶ
かなり便利な気がする。
class Hoge: def __init__(self, array): self.array = array def parse(self, suf=""): while len(self.array) > 0: s = self.array.pop(0) if s=="(": string = s+suf in_paren = (i for i in self.parse(suf+"x")) yield string for i in in_paren: yield i elif s== ")": yield s+suf return else: yield s+suf b = Hoge(["A", "B", "C", "(", "D", "(", "E", ")", ")", "F", "G"]) for i in b.parse(): print i
括弧の対応を再帰的に読むようなジェネレータ。
$ ./hoge.py A B C ( Dx (x Exx )xx )x F G
ちょっとダサいので、もっとかっこいいコードにしてください。