def get_strings(letters, max_length):
for i in range(1, max_length + 1):
for value in product(letters, repeat=i):
yield "".join(value)
comb = [i for i in get_strings(ascii_lowercase, 4)]
print "# of possible combinations: %s" % len(comb)
def perc(i, tot):
p = float(i) /float(tot)
return p * 100
marker = [x for x in range(100) if x % 10 == 0]
marker.pop(0)
# make it 10:0, 20:0, 30:0, 40:0 and so forth...
mark = dict(zip(marker, [0 for i in range(len(marker))]))
print "BEFORE:"
for i in marker:
print "%s percent index: %s" % (i, mark[i])
l = len(comb)
for i,v in enumerate(comb):
p = perc(i, l)
ip = math.ceil(p)
if ip in marker:
mark[ip] = i
print "AFTER:"
for i in marker:
print "%s percent index: %s" % (i, mark[i])
OUTPUT:獲取指數
# of possible combinations: 475254
BEFORE:
10 percent index: 0
20 percent index: 0
30 percent index: 0
40 percent index: 0
50 percent index: 0
60 percent index: 0
70 percent index: 0
80 percent index: 0
90 percent index: 0
AFTER:
10 percent index: 47525
20 percent index: 95050
30 percent index: 142576
40 percent index: 190101
50 percent index: 237627
60 percent index: 285152
70 percent index: 332677
80 percent index: 380203
90 percent index: 427728
我能與上面的代碼來做到這一點,但它似乎很繁瑣和大量的不必要的步驟(或者說可以合併或減少)。
任何簡化?
什麼是get_strings?還有,你爲什麼使用'for i,v in enumerate(comb)'? – 2015-04-05 17:04:41
@PadraicCunningham增加了get_strings – ealeon 2015-04-05 17:05:49