在許多情況下,我們必須從單個字元串或不同的數字集合中找到不同的組合。
為了在 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/zh-tw/n/130081.html