Combinatorial iterators in Python

Python Methods and Functions

There are 4 combinatorial iterators in python:

  1. Product()

    This tool computes the Cartesian product of the input iterations. To compute the product of an iterable with itself, we use the optional repeat keyword argument to specify the number of repetitions. The output of this function is —  tuples in sorted order.

    Syntax :

     product (iterables *, repeat = 1) 

    Examples :

    # import product function from itertools module

    from itertools import product

     

    print ( "The cartesian product using repeat:" )

    print ( list (product ([ 1 , 2 ], repeat = 2 )))

    print ()

     

    print ( "The cartesian product of the containers:" )

    print ( list (product ([ ' geeks' , 'for' , 'geeks' ], ' 2' )))

    print ()

      

    print ( "The cartesian product of the containers:" )

    print ( list (product ( ' AB' , [ 3 , 4 ] )))

    Output:

     The cartesian product using repeat: [(1, 1), (1, 2), (2, 1), (2, 2)] The cartesian product of the containers: [('geeks' , '2'), (' for', '2'), (' geeks', '2')] The cartesian product of the containers: [(' A', 3), ('A', 4), ('B', 3), (' B', 4)] 
  2. Permutations ()

    Permutations () since the name speaks for itself, it is used to generate all the possible permutations of the iterable. All elements are considered unique based on their position and not on their meaning. This function accepts an iterable and a group_size, if the group_size value is not specified or is None then the group_size value becomes the length of the iterable.

    Syntax :

     permutations (iterables *, group_size = None) 

    Example :

    # import product function from itertools module

    from itertools import permutations

     

    print ( "All the permutations of the given list is:"

    print ( list (permutations ([ 1 , 'geeks' ], 2 )))

    print ()

     

    print ( "All the permutations of the given string is:"

    print ( list (permutations ( 'AB' )))

    print ()

     

    print ( "All the permutations of the given container is: "

    print ( list (permutations ( range ( 3 ), 2 )))

    Exit:

     All the permutations of the given list is: [(1, 'geeks'), ( 'geeks', 1)] All the permutations of the given string is: [(' A', 'B'), (' B', 'A')] All the permutations of the given container is: [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)] 
  3. Combinations () :

    This iterator prints all possible combinations (no replacement) of the container passed in arguments, in the specified group size, in sorted order.

    Syntax :

     combinations (iterables *, group_size) 

    Examples :

    # import combinations from the itertools module

     

    from itertools import combinations

      

    print ( "All the combination of list in sorted order (without replacement) is: "

    print ( list (combinations ([ ' A' , 2 ], 2 )))

    print ()

    < p>  

    print ( "All the combination of string in sorted order (without replacement) is:" )

    print ( list (combinations ( 'AB' , 2 ) ))

    print ()

     

    print ( "All the combination of list in sorted order (without replacement) is:" )

    print ( list (combinations ( range < / code> ( 2 ), 1 )))

    Exit :

     All the combination of list in sorted order (without replacement) is: [('A', 2)] All the combination of string in sorted order (without replacement) is: [('A',' B')] All the combination of list in sorted order (without replacement) is: [(0,), (1,)] 
  4. Combinations_with_replacement ():

    This function returns a subsequence of length n from the items being iterated over, where n — an argument that the function takes, specifying the length of the subsequences generated by the function. Individual elements may be repeated in the combination combination function.

    Syntax :

     combinations_with_replacement (iterables *, n = None) 

    Examples :

    # import combinations from the itertools module

     

    from itertools import combinations_with_replacement

      

    print ( "All the combination of string in sorted order (with replacement) is:" )

    print ( list (combinations_ with_replacement ( "AB" , 2 )))

    print ()

     

    print ( "All the combination of list in sorted order (with replacement) is:" )

    print ( list (combinations_with_replacement ([ 1 , 2 ] , 2 )))

    print ()

     

    print ( " All the combination of container in sorted order (with replacement) is: " )

    print ( list (combinations_with_replacement ( range ( 2 ), 1 )))

    Exit:

     All the combination of string in sorted order (with replacement) is: [('A',' A '), (' A', 'B'), (' B', 'B')] All the combination of list in sorted order (with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order (with replacement) is: [(0,), (1,)] 




Get Solution for free from DataCamp guru