首頁 > 後端開發 > Python教學 > 使用Python進行兩個字串的並集操作

使用Python進行兩個字串的並集操作

王林
發布: 2023-08-19 08:58:26
轉載
1516 人瀏覽過

使用Python進行兩個字串的並集操作

Python是全球程式設計師常用的語言之一,用於各種不同的目的,如機器學習、資料科學、Web開發以及執行許多其他自動化操作。它具有許多不同的功能,可以幫助我們在許多不同的專案上工作。 Python的一個特性就是聯合操作。聯合運算是指將兩個不同的字串合併為一個公用字串,同時刪除兩個字串中的所有公用元素。在本文中,我們將學習可以用於兩個字串的聯合操作的不同方法。

聯合運算的不同方法

Sets

的中文翻譯是:

集合

Sets是Python中提供的一種功能,用於將多個項儲存在一個資料集中。它具有一個內建功能,可以從字串中刪除所有共同的元素。讓我們舉一個例子來更好地理解它:

Example

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字典進行並集操作。字典將用於將所有資料儲存為字串,然後對其進行並集操作。透過這種方法進行並集操作的範例如下:

Example

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
登入後複製

輸出

The output of the above example will look as follows:

Whater 
登入後複製

檢查清單和會員資格

這是一個非常簡單的執行並集操作的方法。我們只需要將字串轉換為列表進行並集操作。這種方法的範例如下:

Example

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
登入後複製

輸出

The output of the above example will look as follows:

Whater  
登入後複製

Set和Pipe操作符的組合使用

這個方法是一種複雜的方法,不應該在簡單的組合情況下使用。在這個方法中,字串被轉換成集合,然後我們將使用管道運算子而不是直接使用並集。讓我們舉個例子來更好地理解:

Example

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模組用於有效率地檢查資料集中的所有循環。它有許多不同的函數,可以用於許多不同的目的。我們將使用兩個不同的函數來執行並集操作。讓我們透過一個例子來更好地理解:

Example

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
登入後複製

輸出

The output of the above example will look as follows:

Wraeth 
登入後複製
登入後複製

結論

了解可以用於執行並集操作的不同方法非常重要。本文介紹了可以用於執行並集操作的不同方法。根據方便和應用領域,可以使用上述任何方法之一。

以上是使用Python進行兩個字串的並集操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板