我有一个像这样的嵌套循环:
for a in b.keys():
for c in b[a]:
out = '%-30s %s' % (c, a)
space = out.count(' ')
split = space * '-'
print '\t%-30s %s %-10s' % (c, split, a)
结果是:
test1 ------- something1
test23sdfsf ----- dsffdgdgfddfsdf
但我想要的是:
test1 -------------- something1
test23sdfsf -------- dsffdgdgfddfsdf
答案 0 :(得分:3)
如果您想使用最大宽度30
,请执行以下操作:
for a in b.keys():
for c in b[a]:
print("%s %s" % (c.ljust(30, '-'), a))
答案 1 :(得分:0)
如何使用以下内容:
Test: {0:15} Something {1}".format(Variable,AnotherVar)
您可以根据长度计算数字。
添加更多示例:
>>> a="test"
>>> b="another_one"
>>> "A: {0:20} B {1}".format(a,b) 'A: test B another_one'
>>> "A: {0:15} B {1}".format(a,b) 'A: test B another_one'
>>> "A: {0:4} B {1}".format(a,b) 'A: test B another_one'
您还可以根据要打印的字符串长度,在第一个{}中使用变量作为第二个数字。
答案 2 :(得分:0)
您可以在nested arguments and complex examples
中查看我的解决方案的相同示例for a in b.keys():
for c in b[a]:
print '{key:{fill}{align}20} {item}'.format(key=key + ' ', fill='-', align='<', item=b[key])