Python中两个字符串的并集运算


Python 是一种全球程序员广泛使用的语言,用于机器学习、数据科学、Web 开发以及许多其他自动化操作。它具有许多不同的功能,可以帮助我们处理许多不同的项目。Python 的一项这样的功能就是并集运算。并集运算意味着将两个不同的字符串组合成一个公共字符串,同时去除两个字符串中所有公共元素。在本文中,我们将学习可用于两个字符串并集运算的不同方法。

并集运算的不同方法

集合

集合是 Python 中提供的一种特性,用于在一个单一数据集中存储多个项。它具有去除字符串中所有公共元素的内置功能。让我们来看一个例子以便更好地理解。

示例

Open Compiler
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 字典进行并集运算。字典将用于存储所有数据作为字符串,然后对其执行并集运算。此方法的并集运算示例如下所示

示例

Open Compiler
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 

检查列表和成员资格

这是一种非常简单的执行并集运算的方法。我们将简单地将字符串转换为列表以进行并集运算。此方法的示例如下所示

示例

Open Compiler
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  

集合和管道运算符的组合使用

此方法是一种复杂的方法,不应在简单的组合情况下使用。在此方法中,字符串被转换为集合,然后我们不直接使用并集,而是使用管道运算符。让我们来看一个例子以便更好地理解。

示例

Open Compiler
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 

结论

了解可用于执行并集运算的不同方法非常重要。本文介绍了可用于执行并集运算的不同方法。可以根据方便性和应用领域使用上述任何一种方法。

更新于:2023年8月2日

454 次查看

开启你的职业生涯

通过完成课程获得认证

开始
广告