s = ['0.3','1.25','12.98','2']
s=map(lambda x:format(float(x),'>5.2f'),s)
print(list(s))
#[' 0.30', ' 1.25', '12.98', ' 2.00']
1
explist OP 有没有什么格式可以一步到位的?
|
2
suspended 2016-12-04 18:28:12 +08:00
看我大 ruby 一句话搞掂:
```ruby irb(main):007:0> ['0.3','1.25','12.98','2'].map {|x| '% 2.2f' % x.to_f} => [" 0.30", " 1.25", " 12.98", " 2.00"] ``` |
3
suspended 2016-12-04 18:29:25 +08:00
搞错。。。啊,不能删 /改回复? - -!
|
5
Kisesy 2016-12-04 18:44:55 +08:00
print(['%5.2f'%float(x) for x in ['0.3','1.25','12.98','2']])
一行到位 |
6
explist OP 直接用格式化的相关函数如 format(),zfill()等
|
7
lonelinsky 2016-12-04 23:02:08 +08:00
[ '{: >5.2f}'.format(float(x)) for x in s ] 这个应该可以
|