Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. values = [
  2. ('a', 'b', 'c'),
  3. ('d', 'e'),
  4. ('f', 'g', 'h')
  5. ]
  6.  
  7. combinations = [
  8. ('a', 'd', 'f'),
  9. ('a', 'e', 'g'),
  10. ('a', 'e', 'h'),
  11. ('b', 'd', 'f'),
  12. ('b', 'e', 'g'),
  13. ('b', 'e', 'h'),
  14. ('c', 'd', 'f'),
  15. ('c', 'e', 'g'),
  16. ('c', 'e', 'h')
  17. ]
  18.  
  19. values = [
  20. ('a', 'b', 'c'),
  21. ('d', 'e'),
  22. ('f', 'g', 'h'),
  23. ('i', 'j', 'k', 'l')
  24. ]
  25.  
  26. combinations = [
  27. ('a', 'd', 'f', 'i'),
  28. ('a', 'e', 'g', 'j'),
  29. ('a', 'e', 'h', 'k'),
  30. ('a', 'e', 'h', 'l'),
  31. ('b', 'd', 'f', 'i'),
  32. ('b', 'e', 'g', 'j'),
  33. ('b', 'e', 'h', 'k'),
  34. ('b', 'e', 'h', 'l'),
  35. ('c', 'd', 'f', 'i'),
  36. ('c', 'e', 'g', 'j'),
  37. ('c', 'e', 'h', 'k'),
  38. ('c', 'e', 'h', 'l'),
  39.  
  40. ]
  41.  
  42. from itertools import product
  43.  
  44.  
  45. def extend_to_max_len(tup, length):
  46. '''extends a tuple to a specified length by
  47. filling the empty spaces with last element of given tuple
  48. '''
  49. fill_count = length - len(tup)
  50. return (*tup, *[tup[-1]]*fill_count)
  51.  
  52.  
  53. def non_cartesian_sum(values):
  54. '''Expects a list of tuples.
  55. gives the output according to the custom rules:
  56. top: first row: to be used for cartesian product with zip of remaining rows
  57. bottom: remaining rows: extended to longest length before zipping
  58. '''
  59. if len(values) < 2:
  60. print("Check length of input provided")
  61. return None
  62. top = values[0]
  63. bottom = values[1:]
  64. max_len = max(len(row) for row in bottom)
  65. bottom = [extend_to_max_len(row, max_len) for row in bottom]
  66. out = [(first, *rest) for first, rest in product(top, zip(*bottom))]
  67. return out
  68.  
  69.  
  70. values = [
  71. ('a', 'b', 'c'),
  72. ('d', 'e'),
  73. ('f', 'g', 'h'),
  74. ('i', 'j', 'k', 'l')
  75. ]
  76.  
  77. out = non_cartesian_sum(values)
  78. print(out)
  79.  
  80. [('a', 'd', 'f', 'i'),
  81. ('a', 'e', 'g', 'j'),
  82. ('a', 'e', 'h', 'k'),
  83. ('a', 'e', 'h', 'l'),
  84. ('b', 'd', 'f', 'i'),
  85. ('b', 'e', 'g', 'j'),
  86. ('b', 'e', 'h', 'k'),
  87. ('b', 'e', 'h', 'l'),
  88. ('c', 'd', 'f', 'i'),
  89. ('c', 'e', 'g', 'j'),
  90. ('c', 'e', 'h', 'k'),
  91. ('c', 'e', 'h', 'l')]
  92.  
  93. values = [
  94. ('a', 'b', 'c'),
  95. ('d', 'e'),
  96. ('f', 'g', 'h')
  97. ]
  98.  
  99.  
  100. length_of_1 = len(values[1])
  101. length_of_2 = len(values[2])
  102.  
  103. output = []
  104. for item0 in values[0]:
  105. for i in range(max(length_of_1, length_of_2)):
  106. if i >= length_of_1:
  107. item1 = values[1][-1]
  108. else:
  109. item1 = values[1][i]
  110. if i >= length_of_2:
  111. item2 = values[2][-1]
  112. else:
  113. item2 = values[2][i]
  114.  
  115. triple = (item0, item1, item2)
  116. output.append(triple)
  117.  
  118. for tup in output:
  119. print(tup)
  120.  
  121. values = [
  122. ('a', 'b', 'c'),
  123. ('d', 'e'),
  124. ('f', 'g', 'h')
  125. ]
  126. combination = [(a,b,c) for a in values[0] for b in values[1] for c in values[2]]
  127. print(combination)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement