开发者

how to get this string using python

i have a list like this :

a=[1000,200,30]

and i want to get a list开发者_高级运维 like this :

['01000','00200','00030']

so what can i do ,

thanks


>>> a=[1000,200,30]
>>> [str(e).zfill(5) for e in a]
['01000', '00200', '00030']

str.zfill


str.format() is the preferred way to do this if you are using Python >=2.6

>>> a=[1000, 200, 30]
>>> map("{0:05}".format, a)
['01000', '00200', '00030']


You can do it like this:

a = [1000,200,30]
b = ["%05d" % (i) for i in a]
print b

The number tells the width and the leading zero says that you want leading zeros.


map(lambda x:str(x).zfill(5),a)


Look at formatting strings in Python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜