Welcome, guest | Sign In | My Account | Store | Cart
def nestedSplit(astring, sep=None, *subsep):
    """nestedSplit(astring, sep=None, *subsep): given astring, and one or more split
    strings, it splits astring hierarchically. The first split key is the higher level one.
    Ex.: nestedSplit("a b\nc d", "\n", " ") => [['a', 'b'], ['c', 'd']] """
    if subsep:
        return [nestedSplit(fragment, *subsep) for fragment in astring.split(sep)]
    return astring.split(sep)


if __name__ == '__main__':
    st = "a b\nc d"
    print st
    print nestedSplit(st, "\n", " ")
    print

    tetris = """\
    ....
    .##.
    .##.
    ....

    ####
    ####
    ..##
    ..##"""

    from textwrap import dedent
    tetris = dedent(tetris)
    print tetris
    print nestedSplit(tetris, "\n\n", "\n")

History

  • revision 4 (18 years ago)
  • previous revisions are not available