Python all possible tuples from list

I'll bet itertools-based solutions are going to be faster, but if they need to be avoided [e.g. stuck on Python 2.5 without itertools.product, etc], it can of course be entirely coded in "base Python" when one must.

Trying to "pull out all the stops" for speed, maybe something like:

def odo[*names_and_valuelists]:
    aux = [[vl, 0] for n, vl in names_and_valuelists]
    if any[len[vl]==0 for vl, _ in aux]:
        return
    while True:
        yield tuple[vl[i] for vl, i in aux]
        for vlandi in reversed[aux]:
          if vlandi[1] == len[vlandi[0]]-1:
            vlandi[1] = 0
          else:
            vlandi[1] += 1
            break
        else:
          return

though minor tweaks might still accelerate it [needs thorough profiling with realistic sample data!].

Here's your example of use:

def main[]:
    data = [
        ['Col1', 'value11 value12 value13'.split[]],
        ['Col2', 'value21 value22'.split[]],
        ['Col3', 'value31 value32 value33'.split[]],
    ]
    for tup in odo[data[0], data[1]]: print tup
    print
    for tup in odo[data[1], data[2]]: print tup
    print
    for i, tup in enumerate[odo[*data]]:
        print tup
        if i>5: break

if __name__ == '__main__':
    main[]

which emits the results:

['value11', 'value21']
['value11', 'value22']
['value12', 'value21']
['value12', 'value22']
['value13', 'value21']
['value13', 'value22']

['value21', 'value31']
['value21', 'value32']
['value21', 'value33']
['value22', 'value31']
['value22', 'value32']
['value22', 'value33']

['value11', 'value21', 'value31']
['value11', 'value21', 'value32']
['value11', 'value21', 'value33']
['value11', 'value22', 'value31']
['value11', 'value22', 'value32']
['value11', 'value22', 'value33']
['value12', 'value21', 'value31']

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let’s discuss certain ways in which this problem can be solved.

    Method #1 : Using list comprehension + product[]
    This task can be performed using list comprehension which can be used to iterate the container of N numbers and product[] performs the task of formation of combinations from them.

    Python3

    from itertools import product

    N = 3

    res = [ele for ele in product[range[1, N + 1], repeat = N]]

    print["Tuple Combinations till N are : " + str[res]]

    Output :

    Tuple Combinations till N are : [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]

    Method #2 : Using product[]
    This task can also be performed by using just the single function. The use of list comprehension can be eliminated using the conversion to list.

    Python3

    from itertools import product

    N = 3

    res = list[product[range[1, N + 1], repeat = N]]

    print["Tuple Combinations till N are : " + str[res]]

    Output :

    Tuple Combinations till N are : [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]


    Given a list, the task is to write a python program that can split it into all possible tuple pairs combinations. 

    Input : test_list = [4, 7, 5, 1, 9]

    Output : [[4, 7, 5, 1, 9], [4, 7, 5, [1, 9]], [4, 7, [5, 1], 9], [4, 7, [5, 9], 1], [4, [7, 5], 1, 9], [4, [7, 5], [1, 9]], [4, [7, 1], 5, 9], [4, [7, 1], [5, 9]], [4, [7, 9], 5, 1], [4, [7, 9], [5, 1]], [[4, 7], 5, 1, 9], [[4, 7], 5, [1, 9]], [[4, 7], [5, 1], 9], [[4, 7], [5, 9], 1], [[4, 5], 7, 1, 9], [[4, 5], 7, [1, 9]], [[4, 5], [7, 1], 9], [[4, 5], [7, 9], 1], [[4, 1], 7, 5, 9], [[4, 1], 7, [5, 9]], [[4, 1], [7, 5], 9], [[4, 1], [7, 9], 5], [[4, 9], 7, 5, 1], [[4, 9], 7, [5, 1]], [[4, 9], [7, 5], 1], [[4, 9], [7, 1], 5]]

    Explanation : All pairs partitions are formed.

    Input : test_list = [4, 7, 5, 1]

    Output : [[4, 7, 5, 1], [4, 7, [5, 1]], [4, [7, 5], 1], [4, [7, 1], 5], [[4, 7], 5, 1], [[4, 7], [5, 1]], [[4, 5], 7, 1], [[4, 5], [7, 1]], [[4, 1], 7, 5], [[4, 1], [7, 5]]]

    Explanation : All pairs partitions are formed.

    In this, we perform all pair creation from 1st element, and using recursion multiple elements are converted into pairs by appropriate partitioning.

    The original list is : [4, 7, 5, 1]

    Created partitions : [[4, 7, 5, 1], [4, 7, [5, 1]], [4, [7, 5], 1], [4, [7, 1], 5], [[4, 7], 5, 1], [[4, 7], [5, 1]], [[4, 5], 7, 1], [[4, 5], [7, 1]], [[4, 1], 7, 5], [[4, 1], [7, 5]]]

    Chủ Đề