Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- values = [
- ('a', 'b', 'c'),
- ('d', 'e'),
- ('f', 'g', 'h')
- ]
- combinations = [
- ('a', 'd', 'f'),
- ('a', 'e', 'g'),
- ('a', 'e', 'h'),
- ('b', 'd', 'f'),
- ('b', 'e', 'g'),
- ('b', 'e', 'h'),
- ('c', 'd', 'f'),
- ('c', 'e', 'g'),
- ('c', 'e', 'h')
- ]
- values = [
- ('a', 'b', 'c'),
- ('d', 'e'),
- ('f', 'g', 'h'),
- ('i', 'j', 'k', 'l')
- ]
- combinations = [
- ('a', 'd', 'f', 'i'),
- ('a', 'e', 'g', 'j'),
- ('a', 'e', 'h', 'k'),
- ('a', 'e', 'h', 'l'),
- ('b', 'd', 'f', 'i'),
- ('b', 'e', 'g', 'j'),
- ('b', 'e', 'h', 'k'),
- ('b', 'e', 'h', 'l'),
- ('c', 'd', 'f', 'i'),
- ('c', 'e', 'g', 'j'),
- ('c', 'e', 'h', 'k'),
- ('c', 'e', 'h', 'l'),
- ]
- from itertools import product
- def extend_to_max_len(tup, length):
- '''extends a tuple to a specified length by
- filling the empty spaces with last element of given tuple
- '''
- fill_count = length - len(tup)
- return (*tup, *[tup[-1]]*fill_count)
- def non_cartesian_sum(values):
- '''Expects a list of tuples.
- gives the output according to the custom rules:
- top: first row: to be used for cartesian product with zip of remaining rows
- bottom: remaining rows: extended to longest length before zipping
- '''
- if len(values) < 2:
- print("Check length of input provided")
- return None
- top = values[0]
- bottom = values[1:]
- max_len = max(len(row) for row in bottom)
- bottom = [extend_to_max_len(row, max_len) for row in bottom]
- out = [(first, *rest) for first, rest in product(top, zip(*bottom))]
- return out
- values = [
- ('a', 'b', 'c'),
- ('d', 'e'),
- ('f', 'g', 'h'),
- ('i', 'j', 'k', 'l')
- ]
- out = non_cartesian_sum(values)
- print(out)
- [('a', 'd', 'f', 'i'),
- ('a', 'e', 'g', 'j'),
- ('a', 'e', 'h', 'k'),
- ('a', 'e', 'h', 'l'),
- ('b', 'd', 'f', 'i'),
- ('b', 'e', 'g', 'j'),
- ('b', 'e', 'h', 'k'),
- ('b', 'e', 'h', 'l'),
- ('c', 'd', 'f', 'i'),
- ('c', 'e', 'g', 'j'),
- ('c', 'e', 'h', 'k'),
- ('c', 'e', 'h', 'l')]
- values = [
- ('a', 'b', 'c'),
- ('d', 'e'),
- ('f', 'g', 'h')
- ]
- length_of_1 = len(values[1])
- length_of_2 = len(values[2])
- output = []
- for item0 in values[0]:
- for i in range(max(length_of_1, length_of_2)):
- if i >= length_of_1:
- item1 = values[1][-1]
- else:
- item1 = values[1][i]
- if i >= length_of_2:
- item2 = values[2][-1]
- else:
- item2 = values[2][i]
- triple = (item0, item1, item2)
- output.append(triple)
- for tup in output:
- print(tup)
- values = [
- ('a', 'b', 'c'),
- ('d', 'e'),
- ('f', 'g', 'h')
- ]
- combination = [(a,b,c) for a in values[0] for b in values[1] for c in values[2]]
- print(combination)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement