我有这个代码(在字符串中打印所有排列的出现)
def splitter(str):
for i in range(1, len(str)):
start = str[0:i]
end = str[i:]
yield (start, end)
for split in splitter(end):
result = [start]
result.extend(split)
yield result
el =[];
string = "abcd"
for b in splitter("abcd"):
el.extend(b);
unique = sorted(set(el));
for prefix in unique:
if prefix != "":
print "value " , prefix , "- num of occurrences = " , string.count(str(prefix));
Run Code Online (Sandbox Code Playgroud)
我想打印字符串变量中的所有排列事件.
因为排列不是相同的长度我想要修复宽度并打印出一个不喜欢这个的好处:
value a - num of occurrences = 1
value ab - num of occurrences = 1 …Run Code Online (Sandbox Code Playgroud) 我最近(终于?)开始使用.format()并且有一个可能有点模糊的问题.
特定
res = ['Irene Adler', 35, 24.798]
Run Code Online (Sandbox Code Playgroud)
和
(1) print('{0[0]:10s} {0[1]:5d} {0[2]:.2f}'.format(res))
(2) print('{:{}s} {:{}d} {:{}f}'.format(res[0], 10, res[1], 5, res[2], .2))
Run Code Online (Sandbox Code Playgroud)
工作得很好,同时印刷:
Irene Adler 35 24.80
Irene Adler 35 24.80
Run Code Online (Sandbox Code Playgroud)
我不知道我可以像(1)那样处理清单.我之前看到过带有旧%格式的字段宽度参数(2).
我的问题是想要做这样的事情,它结合了(1)和(2):
(3) print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, 10, 5, .2))
Run Code Online (Sandbox Code Playgroud)
但是,我无法做到这一点,我无法从文档中找出这是否可行.提供要打印的列表和宽度的参数会很好.
顺便说一句,我也试过这个(没有运气):
args = (10, 5, .2)
(4) print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, args))
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我得到:
D:\Users\blabla\Desktop>python tmp.py
Traceback (most recent call last):
File "tmp.py", line 27, in <module>
print('{0[0]:{}s} {0[1]:{}d} {0[2]:{}f}'.format(res, 10, 5, .2))
ValueError: cannot switch from …Run Code Online (Sandbox Code Playgroud) 我无法正确对齐python打印文本.我已经尝试过我所知道的一切,但结果仍然相同而且非常烦人!
这是我在控制台中获得的内容
这是我的代码.
print " FileName\t\t\t\t\tStatus\t\tBinary Type\n"
for files in PASS:
log = subprocess.check_output(['dumpbin','/HEADERS',files])
if arch64 in log:
print" %s \t\t\t\tPASSED\t\t 64-bit \t\t " %files
elif arch32 in log:
print" %s \t\t\t\tPASSED\t\t 32-bit \t\t " %files
print"\n"
for files in FAILED:
print" %s \t\t\t\t FAILED \t\t " %files
print "\n\n
Run Code Online (Sandbox Code Playgroud)