Python 对两个字符串进行并集运算

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

使用Set和Pipe操作符的组合使用

这种方法是一种复杂的方法,在简单的组合情况下不应使用。在这种方法中,字符串被转换为集合,然后我们将使用管道操作符而不是直接使用union。让我们通过一个例子来更好地理解:

示例

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

结论

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程