我有这段代码(在字符串中打印所有排列的出现)
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));
我想打印字符串可变项中所有出现的排列。
由于排列的长度不同,所以我想固定宽度并以一种不太好的方式打印它:
value a - num of occurrences = 1
value ab - num of occurrences = 1
value abc - num of occurrences = 1
value b - num of occurrences = 1
value bc - num of occurrences = 1
value bcd - num of occurrences = 1
value c - num of occurrences = 1
value cd - num of occurrences = 1
value d - num of occurrences = 1
我该如何使用format
呢?
我找到了这些帖子,但与字母数字字符串的配合并不理想: