I have a hierarchical descriptor string that looks like foo:bar:baz where elements in the hierarchy are delimited by :, and I would like to iterate through the hierarchy levels. Is there an easy way to do this, something easier than this:
def hierarchy(s):
segments = s.split(':')
for i in range(len(segments)):
prefix = ':'.join(segments[0:i+1])
print prefix
# or do something else instead of prefix
How about: