Python中兩個字串的並集運算
Python 是一種全球程式設計師廣泛使用的語言,用於機器學習、資料科學、Web 開發以及許多其他自動化操作。它具有許多不同的功能,可以幫助我們處理許多不同的專案。Python 的一項這樣的功能就是並集運算。並集運算意味著將兩個不同的字串組合成一個公共字串,同時去除兩個字串中所有公共元素。在本文中,我們將學習可用於兩個字串並集運算的不同方法。
並集運算的不同方法
集合
集合是 Python 中提供的一種特性,用於在一個單一資料集中儲存多個項。它具有去除字串中所有公共元素的內建功能。讓我們來看一個例子以便更好地理解。
示例
def multiple_strings(first, second): # The input of both the strings are given data1 = set(first) # Both the strings are then converted into data sets data2 = set(second) union_data = data1.union(data2) # After conversion, the data sets are combined with the help of union operation final_string = ''.join(union_data) # The Combined data set is then converted back into strings return final_string # Example first = "What" # The two input strings are defined second = "Where" final_result = multiple_strings(first, second) # The function multiple_strings is run print(final_result) # The output after union Operation Will be shown
輸出
上述示例的輸出如下所示
Wraeth
字典
在這種方法中,我們將使用 Python 字典進行並集運算。字典將用於儲存所有資料作為字串,然後對其執行並集運算。此方法的並集運算示例如下所示
示例
def multiple_strings(first, second): # The input of both the strings are given union_dict = {} # A new dictionary is created for the union operation for char in first: # All the elements in both the strings are checked and then they are added in the new dictionary created union_dict[char] = True for char in second: union_dict[char] = True # No duplicate characters will be added because dictionary keys will take input of different characters only union_string = ''.join(union_dict.keys()) # Once the union operation of the keys is performed, then we will convert the dictionary key back into string return union_string # Example first = "What" # The two input strings are defined second = "Where" final_result = multiple_strings(first, second) # The function multiple_strings is run print(final_result) # The output after union Operation Will be shown
輸出
上述示例的輸出如下所示
Whater
檢查列表和成員資格
這是一種非常簡單的執行並集運算的方法。我們將簡單地將字串轉換為列表以進行並集運算。此方法的示例如下所示
示例
def multiple_strings(first, second): # The input of both the string is given combined_strings = list(first) # The first string is converted into a list for char in second: #If the element in second string is not present in first string then they are combined into the first list and the union operation is performed if char not in combined_strings: combined_strings.append(char) final_string = ''.join(combined_strings) #The lists are then converted back into string return final_string # Example first = "What" # The two input strings are defined second = "Where" final_result = multiple_strings(first, second) # The function multiple_strings is run print(final_result) # The output after union Operation Will be shown
輸出
上述示例的輸出如下所示
Whater
集合和管道運算子的組合使用
此方法是一種複雜的方法,不應在簡單的組合情況下使用。在此方法中,字串被轉換為集合,然後我們不直接使用並集,而是使用管道運算子。讓我們來看一個例子以便更好地理解。
示例
def multiple_strings(first, second): # The input of both the string is given first_set = set(first) # Both the strings are converted into sets second_set = set(second) final_string = ''.join(first_set | second_set) # Using the pipe operator the respective sets are combined after removing the common elements return final_string # Example first = "What" # The two input strings are defined second = "Where" final_result = multiple_strings(first, second) # The function multiple_strings is run print(final_result) # The output after union Operation Will be shown
輸出
上述示例的輸出如下所示
Wraeth
Itertools 模組
Itertools 模組用於有效地檢查資料集中存在的全部迴圈。它具有許多不同的函式,可用於許多不同的目的。我們將使用兩個這樣的不同函式來執行並集運算。讓我們來看一個例子以便更好地理解。
示例
import itertools # Do not forget to import itertools or else error might occur def unique_everseen(iterable, key=None): seen = set() seen_add = seen.add if key is None: # The input of both the string is given for element in itertools.filterfalse(seen.__contains__, iterable):# Through the chain() function we will combine both the strings into cone common string seen_add(element) yield element else: for element in iterable: k = key(element) if k not in seen: seen_add(k) yield element def multiple_strings(first, second): # The input of both the string is given union_string = ''.join(unique_everseen(itertools.chain(string1, string2)))# With the help of unique.everseen() function we will remove all the common elements from the combined string return union_string # Example first = "What" # The two input strings are defined second = "Where" final_result = multiple_strings(first, second) # The function multiple_strings is run print(final_result) # The output after union Operation Will be shown
輸出
上述示例的輸出如下所示
Wraeth
結論
瞭解可用於執行並集運算的不同方法非常重要。本文介紹了可用於執行並集運算的不同方法。可以根據方便性和應用領域使用上述任何一種方法。
廣告