需求:
某个字典存储了一系列属性值,
{
‘lodDist‘:100.0,
‘SmallCull‘:0.04,
‘DistCull‘:500.0,
‘trilinear‘:40
‘farclip‘:477
}
在程序中,我们想以以下工整格式将其内容输出,如何处理?
SmallCull:0.04
farclip :477
lodDist :100.0
Distcull :500.0
trilinear:40
思路:
1、使用字符串的str.ljust(),str.rjust(),str.center()进行左,右,居中对齐
2、使用format()方法,传递类似‘<20‘,‘>20‘,‘^20‘参数完成同样任务
代码:
d = {
‘lodDist‘:100.0,
‘SmallCull‘:0.04,
‘DistCull‘:500.0,
‘trilinear‘:40,
‘farclip‘:477
}
#找出字典中key中长度最长的key
a = max(map(len,d.keys()))
print(type(a))
# 方法一:
for x in d:
print(x.ljust(a),‘:‘,d[x])
# 方法二:
# 将int类型的a,转化成字符串,注意‘<‘+b格式的书写
b = str(a)
for i in d:
print(format(i,‘<‘+b),‘:‘,d[i])
原文:https://www.cnblogs.com/Richardo-M-Q/p/13284244.html