首页 > 编程知识 正文

不使用itertools的 Python 组合

时间:2023-11-26 13:13:05 阅读:309317 作者:CHAF

在许多情况下,我们必须从单个字符串或不同的数字集合中找到不同的组合。

为了在 Python 中找到这样的组合,我们有 itertools 模块,这是寻找不同组合和排列的最常见模块。

这个模块是一个非常有效的工具,可以快速找到所有可能的组合。但是 itertools 模块函数并不是我们可以用来找到组合的唯一可能的方法。

在本教程中,我们将了解在 Python 中不使用 itertools 就可以从字符串中找到不同组合的不同方法。

不使用 itertools 的 Python 组合

在本节中,我们将编写 Python 程序,通过在其中实现几个方法来查找组合。我们将在 Python 程序中使用以下方法:

  • 使用迭代法
  • 使用递归方法

在这两种方法中,首先,我们将查看程序并了解它的工作原理,然后我们将转到解释部分来了解其中使用的实现。

使用迭代法的 Python 组合:

为了在程序中实现迭代方法,我们必须导入 numpy 库来使用它的函数。让我们理解下面的例子。

示例:


# Import numpy module in program
import numpy as np
# Default function to use iterative approach
def RecurCombo(iterArray, num):
 char = tuple(iterArray) # converting input string into a tuple
 m = len(char) # length of string or tuple
 if num > m: # checking if length of combinations is more than length of string
   return
 index = np.arange(num) # using numpy arrange() function
 # Yielding the first sequence
 yield tuple(char[i] for i in index)
 # Using while loop with true condition
 while True:
  # iterating over the tuple we made using reversed for loop
  for a in reversed(range(num)):
     if index[a] != a + m - num:
         break
     else:
              return
  index[a] += 1
  # another for loop iteration
  for b in range(a + 1, num):
   index[b] = index[b - 1] + 1
  yield tuple(char[a] for a in index) # yielding possible combinations from given string
# Taking an input string from user
inputArray = input("Enter an input string to find combinations: ")
# Printing different combinations as result in output
print("All possible combinations of three letter sets from the string given by you is: ")
print([x for x in RecurCombo(inputArray, 3)])

输出:

Enter an input string to find combinations: JavaTpoint
All possible combinations of three letter sets from the string given by you is: 
[('J', 'a', 'v'), ('J', 'a', 'a'), ('J', 'a', 'T'), ('J', 'a', 'p'), ('J', 'a', 'o'), ('J', 'a', 'i'), ('J', 'a', 'n'), ('J', 'a', 't')]

说明:

我们在上面的程序中使用了迭代方法来从输入字符串中找到组合。

首先,我们使用了一个默认的 Python 函数,输入字符串和组合集的长度作为参数。然后,我们将输入字符串转换成元组。我们还检查了组合所需的长度是否不超过字符串的长度。

之后,我们使用 numpy 的 arrange()函数来设置元组的索引。我们将使用索引变量迭代元组。

然后,我们在 While循环中使用反向 for循环和另一个 for循环迭代元组。在循环迭代之后,我们得到了所需长度的可能组合。

然后,我们从用户那里获取了一个输入字符串。最后,我们从输入字符串中返回三个集合的组合。

使用递归方法的 Python 组合:

在递归方法方法中,我们将遍历由字符串列表组成的列表。让我们理解下面的例子。

示例:


# Default Python function to use recursive approach
def RecurCombo(array, num): 
    if num == 0: 
        return [[]] # if length for combination is 0
    l =[] # list to printed in result
    # Using for loop to implement recursive approach
    for j in range(0, len(array)): 
        emptyArray = array[j] # define an empty array to print list of sets
        recurList = array[j + 1:]
        # Recursion method on list defined in function
        for x in RecurCombo(recurList, num-1): 
            l.append([emptyArray]+x) # appending list
    return l # list as result of recursion
if __name__=="__main__":
    # Taking an input string from user
    inputArray = input("Enter an input string to find combinations: ")
    # Printing different combinations as result in output
    print("All possible combinations of three letter sets from the string given by you is: ")
    print(RecurCombo([a for a in inputArray], 3))

输出:

Enter an input string to find combinations: Python
All possible combinations of three letter sets from the string given by you is: 
[['P', 'y', 't'], ['P', 'y', 'h'], ['P', 'y', 'o'], ['P', 'y', 'n'], ['P', 't', 'h'], ['P', 't', 'o'], ['P', 't', 'n'], ['P', 'h', 'o'], ['P', 'h', 'n'], ['P', 'o', 'n'], ['y', 't', 'h'], ['y', 't', 'o'], ['y', 't', 'n'], ['y', 'h', 'o'], ['y', 'h', 'n'], ['y', 'o', 'n'], ['t', 'h', 'o'], ['t', 'h', 'n'], ['t', 'o', 'n'], ['h', 'o', 'n']]

说明:

在上面的程序中,在实现递归方法时,我们没有使用 Python 的任何特定模块。像迭代方法一样,我们使用默认函数在代码中实现递归方法。

在这个程序中,我们使用一个条件来检查组合的所需长度。然后,我们在带有 for循环的函数中使用递归方法。使用递归方法后,我们从输入字符串中返回所需长度的组合。最后,我们将字符串作为用户的输入,并在输出中返回三个集合的组合。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。