使用Python查詢字串的笛卡爾積


在這篇文章中,使用者將學習如何使用Python查詢字串的笛卡爾積。Python語言可以進行字串操作。Python提供了各種內建函式和運算子來對字串執行特定操作。Python語言是一種傑出的語言,主要用於開發滿足使用者特定需求的應用程式。本文介紹了用於查詢給定字串的笛卡爾積的各種方法。笛卡爾積用於Python語言中,以獲取給定字串的所有可能組合。

使用Python查詢字串的笛卡爾積

查詢笛卡爾積的方法或途徑涉及如下列出的三種主要方法。

方法

方法1 - 使用product()函式

方法2 - 使用巢狀for迴圈

方法3 - 使用lambda函式

方法1:使用product()函式查詢字串笛卡爾積的Python程式

為了查詢字串的笛卡爾積,可以使用itertools模組,因為它可以處理各種複雜的函式。

演算法

  • 步驟1 - 定義變數,其值為set1和set2。

  • 步驟2 - 使用product()函式,將給定的兩個集合相乘以形成一個新的字串值。

  • 步驟3 - 字串的可能性在名為“cart_product”的變數中提到,並可視化為列表。

  • 步驟4 - 當執行以下程式碼時,它將返回輸出。

示例

#importing the itertools module to use its function
import itertools

#values are assigned to the variables
set1 = ["welcome", "all", "here"]
set2 = ["see", "you", "soon"]
#product() function is used to get the cartesian product of two given sets
cart_product = list(itertools.product(set1,set2))

#the final output after the multiplication which returns all possibilities
output = [f"{a[0]}{a[1]}" for a in cart_product]
print(output)

輸出

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

方法2:使用巢狀for迴圈查詢字串笛卡爾積的Python程式

巢狀for迴圈用於使用split()、append()和strip()方法迭代列表中的字串。

演算法

  • 步驟1 - 定義變數,其值為set1和set2。

  • 步驟2 - 定義空變數。

  • 步驟3 - 使用product()函式,將給定的兩個集合相乘以形成一個新的字串值。

  • 步驟4 - 使用split()函式,將給定的兩個集合分割以形成一個新的字串值。

  • 步驟5 - 字串的可能性在名為“cart_product”的變數中提到,並作為列表新增。

  • 步驟6 - 當執行以下程式碼時,它將返回輸出。

示例

#values are assigned to the variables
set1 = "welcome, all, here"
set2 = "see, you, soon"

#initializing the empty list
cart_product = []
#for loop is used to iterate through the strings of characters
#split() function is specified with a comma to separate the strings in set1
for a in set1.split(','):
   #split() function is specified with a comma to separate the strings in set2
   for b in set2.split(','):
      #append() is used to add all the combinations of the sets obtained
      cart_product.append(a.strip() + b.strip())

#the final output after the multiplication which returns all possibilities
print(cart_product)

輸出

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

方法3:使用lambda函式查詢字串笛卡爾積的Python程式

lambda函式與map()函式一起使用,當函式未定義或未知時,lambda函式可以有效地使用。

演算法

  • 步驟1 - 定義變數,其值為set1和set2。

  • 步驟2 - 定義空變數。

  • 步驟3 - 使用lambda方法,將給定的兩個集合相乘以形成一個新的字串值。

  • 步驟4 - 使用map()和filter()函式,將給定的兩個集合分割以形成一個新的字串值。

  • 步驟5 - 字串的可能性在名為“cart_product”的變數中提到,並作為列表新增。

  • 步驟6 - 當執行以下程式碼時,它將返回輸出。

示例

#values are assigned to the variables
set1 = "welcome, all, here"
set2 = "see, you, soon"

#using the lambda function along with map() function
cart_product = list(map(lambda a: a[0].strip() + a[1].strip(), [(i,j) for i in set1.split(',') for j in set2.split(',')]))

#the final output after the multiplication which returns all possibilities
print(cart_product)

輸出

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

結論

Python程式廣泛用於處理大量的字串值,為了實現這一點,使用了“itertools”庫。藉助此庫,我們可以直接找到字串元素的笛卡爾積。當用戶瞭解上述方法時,他們可以使用不同的方法來解決給定的問題。

更新於:2023年8月25日

267 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.