📜  Python中的组合迭代器

📅  最后修改于: 2022-05-13 01:54:56.465000             🧑  作者: Mango

Python中的组合迭代器

迭代器是一个可以遍历其所有值的对象。简单地说,迭代器是可以循环的数据类型。生成器是迭代器,但由于它们不能return值,而是在执行时yield结果,使用 'yield'函数。生成器可以像函数一样递归。这些用于简化组合构造(例如排列、组合和笛卡尔积)的递归生成器称为组合迭代器

在Python中有4个组合迭代器:

  1. 产品()

    该工具计算输入迭代的笛卡尔积。为了计算一个可迭代对象与自身的乘积,我们使用可选的repeat关键字参数来指定重复次数。此函数的输出是按排序顺序的tuples

    句法:

    product(iterables*, repeat=1)

    例子:

    # import the 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])))
    

    输出:

    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()用于生成可迭代对象的所有可能排列。所有元素都根据它们的位置而不是它们的值被视为唯一的。该函数采用可迭代对象和 group_size,如果未指定 group_size 的值或等于None ,则 group_size 的值成为可迭代对象的长度。

    句法:

    permutations(iterables*, group_size=None)

    例子:

    # import the 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)))
    

    输出:

    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(iterables*, group_size)

    例子:

    # import combinations from 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()
      
    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(2),1)))
    

    输出:

    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():

    此函数从可迭代的元素中返回长度为 n 的子序列,其中 n 是函数用于确定函数生成的子序列长度的函数。单个元素可以在 combination_with_replacement函数中重复

    句法:

    combinations_with_replacement(iterables*, n=None)

    例子:

    # import combinations from 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)))
    

    输出:

    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,)]