variable length print format

Jeff Epler jepler at unpythonic.net
Tue May 7 15:31:23 EDT 2002


# if your version doesn't have zip,
#zip = lambda *args: apply(map, (None,) + args)

# This is like the new proposed builtin 'enum' except that
# 'base' specifies the ordinal corresponding to the first item
# in the sequence.  Depending on the phase of the moon, range may
# be preferable to xrange
my_enum = lambda seq, base=0: zip(xrange(base, base+len(seq)), seq)

# This is a cheap 'flatten' for two-level structures
l = []
le = l.extend
[le(item) for item in my_enum(List, 1)]

formatstr = "%s:%s\n" * len(List)
print formatstr % tuple(l),

I'm not convinced it'll save much over the simpler
    sys.stdout.writelines(["%s:%s\n" % item for item in my_enum(List, 1)])
but maybe it will.





More information about the Python-list mailing list