Guest User

Untitled

a guest
Sep 30th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #get the nth product of `lists`
  2. def product_by_idx(lists, n):
  3. result = []
  4. for seq in reversed(lists):
  5. n, x = divmod(n, len(seq))
  6. result.append(seq[x])
  7. return result[::-1]
  8.  
  9. #find n such that product(lists, n) == target
  10. def idx_from_product(lists, target):
  11. n = 0
  12. for x, seq in zip(target, lists):
  13. n += seq.index(x)
  14. n *= len(seq)
  15. n //= len(lists[-1])
  16. return n
  17.  
  18. def product(lists, from_, to_):
  19. start = idx_from_product(lists, from_)
  20. end = idx_from_product(lists, to_)
  21. for n in range(start, 1+end):
  22. yield product_by_idx(lists, n)
  23.  
  24. somelists = [
  25. ['A', 'b', 'C', '1'],
  26. ['d', '2', 'A', '4'],
  27. ['c','a', '3', 'g']
  28.  
  29. ]
  30.  
  31. for item in product(somelists, "A23", "A43"):
  32. print("".join(item))
  33.  
  34. """
  35. Result:
  36. A23
  37. A2g
  38. AAc
  39. AAa
  40. AA3
  41. AAg
  42. A4c
  43. A4a
  44. A43
  45. """
Add Comment
Please, Sign In to add comment