python - The Concept Behind itertools's product Function -
so want understand concept of product() function in itertools. mean different between yield , return. , can code shorten down anyway.
def product1(*args, **kwds): pools = map(tuple, args) * kwds.get('repeat', 1) n = len(pools) if n == 0: yield () return if any(len(pool) == 0 pool in pools): return indices = [0] * n yield tuple(pool[i] pool, in zip(pools, indices)) while 1: in reversed(range(n)): # right left if indices[i] == len(pools[i]) - 1: continue indices[i] += 1 j in range(i+1, n): indices[j] = 0 yield tuple(pool[i] pool, in zip(pools, indices)) break else: return
this code should work:
bytes = [i in range(2**(n))] ab= [] obj in bytes: t = str(bin(obj))[2:] t= '0'*(n-len(t)) + t ab.append(t.replace('0','a').replace('1','b'))
n being string size wanted
Comments
Post a Comment