Python教程-Python中的组合(不使用itertools)
在许多情况下,我们需要从单个字符串或不同的数字集中找到不同的组合。
要在Python中查找此类组合,我们有itertools模块,这是查找不同组合和排列的最常见方法。
这个模块是一个非常高效的工具,可以快速找到所有可能的组合。但是itertools模块的函数并不是查找组合的唯一可能方法。
在本教程中,我们将学习在Python中不使用itertools来查找不同组合的不同方法。
在Python中不使用itertools查找组合
在本节中,我们将编写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
inputinputArray = 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: javatiku
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()函数设置元组的索引。我们将使用索引变量迭代元组。
接下来,我们使用逆向for循环和while循环来迭代元组。在循环中迭代后,我们生成所需长度的可能组合。
最后,我们从用户那里获取输入字符串。最后,我们返回了从输入字符串中生成的三个组合。
使用递归方法查找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
inputinputArray = 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循环进行迭代。在使用递归方法后,我们返回了从输入字符串中生成的所需长度的组合。最后,我们从用户那里获取字符串作为输入,并在输出中返回了三个集合的组合。