variable length print format
Brandon Beck
bbeck13 at excite.com
Tue May 7 22:38:52 EDT 2002
Hi Les,
Give this a shot:
lst = ['a', 'b', 'c', 'd']
tuples = [ (i,elem) for (i, elem) in zip(range(1, 1+len(lst)), lst) ]
s = string.join(map(lambda t: "%i:%s"%t, tuples))
print s
Brandon
les_ander at yahoo.com (les ander) wrote in message news:<a2972632.0205071050.9e68201 at posting.google.com>...
> Hi,
> i have a variable length list of numbers.
> i would like to print them out with indicies in front as shown below:
>
> List=['a','b','c','d'] ---> 1:a 2:b 3:c 4:d
>
> one obvious solution is that i do
> for i in range(List):
> print "%s:%s" % (i+1,List[i])
>
> however i have a lot of these and would like it to be faster so i
> tried doing this
>
> formatstr=" %s:%s " * len(List)
>
> print formatstr % (range(1,len(List)+1), List)
>
> but it did not work.
>
> is there a way to do this another way?
> les
More information about the Python-list
mailing list