Python程式向集合追加多個元素
在Python中,集合是一個無序的唯一元素集合,用{}表示。它允許高效的成員資格測試並消除重複值,使其可用於刪除重複項或檢查集合之間公共元素等任務。
向Python中的集合追加多個元素是一種常見操作,它涉及將多個唯一元素新增到現有集合中。
在本文中,我們將學習如何在python中向集合追加多個元素。
示例
假設我們已經獲取了一個輸入集合和一個輸入列表。我們現在將使用上述方法將輸入列表的元素追加到輸入集合中,同時刪除所有重複項並按升序排序。
輸入
inputSet = {3, 9, 5, 1, 2, 8}
newList = [2, 10, 4, 3]
輸出
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}
在上面的輸入集合中,輸入列表的元素被新增到輸入集合中,並透過排序和列印結果按升序去除了所有重複項。
使用的方法
以下是完成此任務的各種方法
使用update()函式
使用管道運算子( | )
使用extend()函式
方法1:使用update()函式
在這種方法中,我們將學習如何在python中使用update()函式向集合追加多個元素。update()函式(將給定的專案,例如字典或具有鍵值對的可迭代物件,插入到字典中)
語法
set_name.update(iterable)
演算法(步驟)
以下是執行所需任務的演算法/步驟
建立一個變數來儲存輸入集合。
列印輸入集合。
建立另一個變數來儲存輸入列表,用於將元素新增到輸入集合中。
使用update()函式透過將輸入列表作為引數傳遞給它來隨機地將輸入列表的元素追加到集合中。
列印追加輸入列表元素後的結果集合
示例
以下程式使用update()函式返回一個集合,該集合透過刪除所有重複項並按順序將輸入列表元素追加到輸入集合中 –
# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# updating the set with the input list elements orderly
inputSet.update(newList)
# printing resultant set after adding input list elements
print("Resultant set after adding list elements:", inputSet)
輸出
執行後,上述程式將生成以下輸出 –
Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}
方法2:使用管道運算子( | )
在這種方法中,我們將學習如何在python中使用管道運算子()函式向集合追加多個元素。管道運算子(|):在內部,管道運算子(|) 呼叫union(),它可用於使用更多新專案更新集合。
語法
result = operand1 | operand2
演算法(步驟)
以下是執行所需任務的演算法/步驟 –。
使用set()函式將給定的列表轉換為集合。
對輸入集合應用|運算子,並將這個新建立的列表元素集合新增到輸入集合中。
列印追加輸入列表元素後的結果集合
示例
以下程式使用管道運算子(|)返回一個集合,該集合透過刪除所有重複項並按順序將輸入列表元素追加到輸入集合中 –
# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# Converting the new list to set
# Applying | operator on the input set to add these set elements to a given set
inputSet |= set(newList)
# printing resultant set after adding input list elements
print("Resultant set after adding list elements:", inputSet)
輸出
執行後,上述程式將生成以下輸出
Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}
方法3:使用extend()函式
在這種方法中,我們將演示如何使用extend()函式向給定集合追加多個元素。extend()函式(將可迭代物件(如列表、元組、字串等)的所有元素新增到列表的末尾)
語法
list_name.extend(iterable)
演算法(步驟)
以下是執行所需任務的演算法/步驟
使用list()函式將給定的輸入集合元素轉換為列表。
將列表作為引數傳遞給extend()函式,以便按順序使用輸入列表元素擴充套件輸入集合元素列表。
使用set()函式(建立一個集合物件。集合列表將以隨機順序顯示,因為專案沒有排序。它刪除所有重複項)將列表轉換為集合。
列印將輸入列表元素新增到輸入集合後的結果集合。
示例
以下程式使用extend()函式返回一個集合,該集合透過刪除所有重複項並按順序將輸入列表元素追加到輸入集合中 –
# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# converting input set into a list
setElementsList = list(inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# extending the set elements list with the input list elements orderly
setElementsList.extend(newList)
# converting the list into a set
# set() function also removes duplicates
resultant_set = set(setElementsList)
# printing the resultant set after adding input list elements
print("Resultant set after adding list elements:", resultant_set)
輸出
執行後,上述程式將生成以下輸出
Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}
結論
在本文中,我們學習了3種不同的方法來向集合追加多個元素。我們學習瞭如何使用update()函式將當前集合與另一個集合相加。我們學習瞭如何將列表轉換為集合,反之亦然。最後,我們學習瞭如何使用extend()函式透過新增另一個列表來擴充套件列表。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP