Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #get the nth product of `lists`
- def product_by_idx(lists, n):
- result = []
- for seq in reversed(lists):
- n, x = divmod(n, len(seq))
- result.append(seq[x])
- return result[::-1]
- #find n such that product(lists, n) == target
- def idx_from_product(lists, target):
- n = 0
- for x, seq in zip(target, lists):
- n += seq.index(x)
- n *= len(seq)
- n //= len(lists[-1])
- return n
- def product(lists, from_, to_):
- start = idx_from_product(lists, from_)
- end = idx_from_product(lists, to_)
- for n in range(start, 1+end):
- yield product_by_idx(lists, n)
- somelists = [
- ['A', 'b', 'C', '1'],
- ['d', '2', 'A', '4'],
- ['c','a', '3', 'g']
- ]
- for item in product(somelists, "A23", "A43"):
- print("".join(item))
- """
- Result:
- A23
- A2g
- AAc
- AAa
- AA3
- AAg
- A4c
- A4a
- A43
- """
Add Comment
Please, Sign In to add comment