使用 Python 將集合拆分為集合列表


在這篇文章中,我們將學習如何在 Python 中將一個集合拆分為一個集合列表。

假設我們已經獲取了一個輸入集合。我們現在將使用下面提到的方法將這個輸入集合逐元素拆分為一個集合列表。

使用的方法

以下是完成此任務所使用的各種方法 -

  • 使用 For 迴圈和 append()、add() 函式

  • 使用 Python map() 和 lambda 函式

  • 使用列表推導式

方法 1:使用 For 迴圈和 append()、add() 函式

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟。 -

  • 建立一個函式listOfSets(),透過接受輸入集合作為引數,將輸入集合逐元素拆分為一個集合列表。

  • 建立一個空列表,用於儲存生成的集合列表。

  • 使用for 迴圈遍歷輸入集合的每個元素。

  • 使用set()函式建立一個空集合。

  • 使用add()函式將輸入集合的當前元素新增到上面建立的空集合中,並將當前元素作為引數傳遞給它。

  • 使用append()函式(將元素新增到列表的末尾)將上述空集合(包含集合的一個元素)追加到輸出列表中。

  • 使用return語句返回生成的集合列表。

  • 建立一個變數來儲存輸入集合。

  • 透過將輸入集合作為引數傳遞給它,呼叫上面定義的listOfSets()函式,以列印生成的集合列表。

示例

以下程式使用 for 迴圈和 append() 函式將輸入集合拆分為一個集合列表 -

# creating a function that breaks the input set
# into a list of sets element-wise by accepting the input set as an argument
def listOfSets(inputSet):
   # creating an empty list for storing a resultant list of sets
   outputList = []
   # traversing through each element of the set
   for k in inputSet:
      # creating an empty set using set()
      emptySet = set()
      # adding the current element of the input set to the above empty set
      emptySet.add(k)
      # appending empty set to the outputList
      outputList.append(emptySet)
   # returning the resultant list of sets
   return(outputList)
# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:", inputSet)
# calling the above listOfSets() function by passing
# the inputSet to it to print the resultant list of sets
print("Breaking the input set into a list of sets:\n", listOfSets(inputSet))

輸出

執行上述程式後,將生成以下輸出 -

The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
 [{'tutorialspoint'}, {'python'}, {'hello'}]

方法 2:使用 Python map() 和 Lambda 函式

Lambda 函式

Lambda 函式,通常稱為“匿名函式”,與普通 Python 函式相同,只是它可以無需名稱即可定義。def 關鍵字用於定義普通函式,而lambda關鍵字用於定義匿名函式。但是,它們僅限於一行表示式。它們與普通函式一樣,可以接受多個引數。

語法

lambda 引數: 表示式

  • 此函式接受任意數量的輸入,但僅計算並返回一個表示式。

  • Lambda 函式可以在需要函式物件的地方使用。

  • 必須記住,lambda 函式在語法上僅限於一個表示式。

Map() 函式

Python 中的 map() 函式在將指定函式應用於指定可迭代物件(如列表、元組等)的每個專案後,返回輸出的 map 物件(迭代器)。

語法

map(function, iterable)

引數

  • 函式 - map 傳遞指定可迭代物件每個元素的函式。

  • 可迭代物件 - 要對映的可迭代物件。

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟。 -

  • 建立一個變數來儲存輸入集合。

  • 使用 lambda 函式訪問集合的所有值,並使用 {} 運算子將值更改為集合。

  • 使用 map() 函式將此條件應用於集合的所有元素。

  • 使用 list() 函式將此集合的集合轉換為集合列表。

  • 列印生成的集合列表。

示例

以下程式使用 map() 和 lambda 函式將輸入集合拆分為一個集合列表 -

# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:", inputSet)
# Modify every element of the set to a separate set using the {} operator.
# Applying this condition to all the elements of the set using a map()
# Converting this result to a list
listOfSets = list(map(lambda k: {k}, inputSet))

# printing the resultant list of sets
print("Breaking the input set into a list of sets:\n", listOfSets)

輸出

執行上述程式後,將生成以下輸出 -

The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
 [{'tutorialspoint'}, {'python'}, {'hello'}]

方法 3:使用列表推導式

列表推導式

當您希望根據現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

示例

以下程式使用列表推導式將輸入集合拆分為一個集合列表 -

# input set
inputSet = {'hello', 'tutorialspoint', 'python'}
# Printing the given set
print("The given set is:",inputSet)
# Traversing through every element of the set and
# converting it to a separate set using the {} operator
listOfSets = [{i} for i in inputSet]

# printing the resultant list of sets
print("Breaking the input set into list of sets:\n", listOfSets)

輸出

執行上述程式後,將生成以下輸出 -

The given set is: {'tutorialspoint', 'python', 'hello'}
Breaking the input set into a list of sets:
 [{'tutorialspoint'}, {'python'}, {'hello'}]

結論

在這篇文章中,我們學習瞭如何使用 3 種不同的方法將給定的集合拆分為一個集合列表。我們還學習瞭如何使用 lambda 函式將特定條件應用於可迭代元素,以及如何使用 map() 函式將此條件應用於可迭代物件的所有元素。

更新時間: 2023 年 1 月 23 日

406 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告