将一个集合拆分为一组集合的Python列表

将一个集合拆分为一组集合的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(function, iterable)

参数

  • function - 一个函数,该函数将指定可迭代对象的每个元素传递给map函数。

  • iterable - 要进行映射的可迭代对象。

步骤

以下是执行所需任务的算法/步骤。

  • 创建一个变量来存储输入集。

  • 使用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()函数将此条件应用于可迭代元素的所有元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程