如何在Python中刪除集合的最後一個元素?
在這篇文章中,我們將學習如何在Python中刪除集合的最後一個元素。
使用的方法
以下是完成此任務的各種方法:
使用discard()函式
使用remove()函式
使用pop()函式
使用列表切片
方法1:使用discard()函式
discard()函式從集合中刪除元素,將元素作為引數傳遞給它。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存包含整數的輸入集合。
使用discard()函式透過將集合的最後一個元素作為引數傳遞給它來刪除集合中的最後一個元素。
列印從輸入集合中刪除最後一個元素後的結果集合。
同樣,檢查包含字串元素的集合。
示例
下面的程式使用discard()函式刪除集合中的最後一個元素:
# input set containing integers inputSet = {5, 7, 2, 1, 8} print("The Given Set is:", inputSet) # removing the last element from the set by passing the # last element as an argument to the discard() function inputSet.discard(8) # printing the resultant set after removing the last element print("Given Set after Removing Last Element", inputSet) # input set containing strings inputSet_1 = {"Hello", "Tutorialspoint", "python"} print("The Given Set is:", inputSet) # similarly removing the last element "python" from the set inputSet_1.discard("python") # printing the resultant set print("Given Set after Removing Last Element", inputSet_1)
輸出
執行後,上述程式將生成以下輸出:
The Given Set is: {1, 2, 5, 7, 8} Given Set after Removing Last Element {1, 2, 5, 7} The Given Set is: {1, 2, 5, 7} Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}
方法2:使用remove()函式
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用remove()函式透過將集合的最後一個元素作為引數傳遞給它來刪除集合中的最後一個元素。
列印從輸入集合中刪除最後一個元素後的結果集合。
示例
下面的程式使用remove()函式刪除集合中的最後一個元素:
# input set containing integers inputSet = {5, 7, 2, 1, 8} print("The Given Set is:", inputSet) # removing the last element from the set by passing the # last element as an argument to the remove() function inputSet.remove(8) # printing the resultant set after removing the last element print("Given Set after Removing Last Element", inputSet) # input set containing strings inputSet_1 = {"Hello", "Tutorialspoint", "python"} print("The Given Set is:", inputSet) # similarly removing the last element "python" from the set inputSet_1.remove("python") # printing the resultant set print("Given Set after Removing Last Element", inputSet_1)
輸出
執行後,上述程式將生成以下輸出:
The Given Set is: {1, 2, 5, 7, 8} Given Set after Removing Last Element {1, 2, 5, 7} The Given Set is: {1, 2, 5, 7} Given Set after Removing Last Element {'Hello', 'Tutorialspoint'}
方法3:使用pop()函式
Python中的pop()方法從集合中刪除隨機元素。如果集合為空,則會引發錯誤。
使用pop()方法從輸入集合中刪除隨機元素。
示例
下面的程式使用pop()函式從輸入集合中刪除隨機元素:
# input set containing integers inputSet = {5, 7, 2, 1, 8} # removing random element from the set using pop() inputSet.pop() # printing the resultant set after removing any random element print(inputSet) # input set containing strings inputSet_1 = {"Hello", "Tutorialspoint", "python"} # similarly removing a random element from the set containing strings using pop() inputSet_1.pop() # printing the resultant set print(inputSet_1)
輸出
執行後,上述程式將生成以下輸出:
{2, 5, 7, 8} {'Hello', 'python'}
方法4:使用列表切片
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用list()函式(返回可迭代物件的列表)將輸入集合轉換為列表,並使用切片(即,獲取除最後一個元素之外的所有元素),然後再次使用set()函式將其轉換為集合來刪除集合中的最後一個元素。
set()函式- 建立一個集合物件。集合列表將以隨機順序顯示,因為專案沒有排序。它刪除所有重複項
列印從輸入集合中刪除最後一個元素後的結果集合。
示例
下面的程式使用列表切片刪除集合中的最後一個元素:
# input set containing integers inputSet = {5, 7, 2, 1, 8} print("The Given Set is:", inputSet) # converting the input set to a list using list() listOfSet = list(inputSet) # Removing the last element using slicing i.e, taking all the elements except the last resultList = listOfSet[:-1] # Converting the result to a set resultSet = set(resultList) # printing the resultant set after removing the last element print("Given Set after Removing Last Element", resultSet) # input set containing strings inputSet_1 = {"Hello", "Tutorialspoint", "python"} print("The Given Set is:", inputSet_1) # similarly removing the last element "python" from the set # using list(),set() and slicing resultSet_1 = set(list(inputSet_1)[:-1]) # printing the resultant set after removing the last element print("Given Set after Removing Last Element", resultSet_1)
輸出
執行後,上述程式將生成以下輸出:
The Given Set is: {1, 2, 5, 7, 8} Given Set after Removing Last Element {1, 2, 5, 7} The Given Set is: {'Tutorialspoint', 'Hello', 'python'} Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}
結論
在本文中,我們介紹瞭如何使用四種不同的方法刪除集合的最後一個元素。我們還學習瞭如何使用pop()函式從集合中刪除隨機元素,以及如何將集合轉換為列表,反之亦然,以及如何對其進行切片。