python Python.Math.Itertools.Permutations.Combinations

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python.Math.Itertools.Permutations.Combinations相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 31 09:49:49 2017

@author: P.Doulgeridis
"""

import itertools

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        print("Combination length required longer than populace")
        return
    indices = list(range(n))
    cycles = list(range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

            
            
def permutations2(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)            
            
a = [ 1, 2, 3, 7, "the" ]
c = [ 5, 6, 8, 9, "ifthen" ]
b = '4567'

# print(list(permutations(b, 3)))
# print(list(permutations(a, 5)))
# print(list(permutations(a, 4)))

# Permutation (Order matters)
#print (list(itertools.permutations(a, 3)))

# Combination (Order does not matter)
#print (list(itertools.combinations(a, 3)))

# Cartesian product (with several iterables - combine each with each one by one)
#print (list(itertools.product(a, c)))

# Cartesian product (with one iterable by itself, repeat factor)
#print (list(itertools.product(a, repeat=4)))


def permutation_generate(input, slots=2):    
    try:
        import itertools
    except ImportError:
        return([],"ImportError: Itertools")
    
    output_list = []
    count = 0
    calc = list(itertools.permutations(input, slots))
    count = len(calc)
    return [count, calc]

def combination_generate(input, slots=2):
    try:
        import itertools
    except ImportError:
        return([],"ImportError: Itertools")
    
    output_list = []
    count = 0
    calc = list(itertools.combinations(input, slots))
    count = len(calc)
    return [count, calc]

def combi_repl_generate(input, slots=2):
    
    checks = True
    
    try:
        import itertools
    except ImportError:
        return([],"ImportError: Itertools")
    
    output_list = []
    count = 0
    calc = list(itertools.combinations_with_replacement(input, slots))
    count = len(calc)
    return [count, calc]   
    
def cartesian_prod_generate(input1, input2):
    try:
        import itertools
    except ImportError:
        return([],"ImportError: Itertools")
    
    output_list = []
    count = 0
    calc = list(itertools.product(a, c))
    count = len(calc)
   
    return [count, calc]
    
def cartesian_prod_repeat(input1, integer):
    try:
        import itertools
    except ImportError:
        return([],"ImportError: Itertools")
    
    output_list = []
    count = 0
    calc = list(itertools.product(a, repeat=integer))
    count = len(calc)
   
    return [count, calc]
    

    
#print (permutation_generate(a, 4))
# print (combination_generate(a, 3))
# print (combi_repl_generate(a, 3))
#print (cartesian_prod_generate(a, c))
#print (cartesian_prod_repeat(c, 2))

以上是关于python Python.Math.Itertools.Permutations.Combinations的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识