不使用itertools的 Python 组合

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

为了在 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循环的函数中使用递归方法。使用递归方法后,我们从输入字符串中返回所需长度的组合。最后,我们将字符串作为用户的输入,并在输出中返回三个集合的组合。


原创文章,作者:简单一点,如若转载,请注明出处:https://www.506064.com/n/130081.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
简单一点简单一点
上一篇 2024-10-03 23:27
下一篇 2024-10-03 23:27

相关推荐

  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29

发表回复

登录后才能评论