Python - 列表列表转换为列表集


当需要将列表列表转化为一组列表时,将使用“映射”、“集”和“列表”方法。

示例

以下对此进行演示

Open Compiler
my_list = [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1,1], [0]] print("The list of lists is: ") print(my_list) my_result = list(map(set, my_list)) print("The resultant list is: ") print(my_result)

输出

The list of lists is:
[[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]]
The resultant list is:
[set([2]), set([1, 2]), set([1, 2, 3]), set([1]), set([0])]

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

说明

  • 定义一个列表列表,并显示在控制台上。

  • 使用“映射”函数将列表转化为一组。

  • 这将再次转化为一个列表。

  • 这被分配给一个变量。

  • 这是作为输出显示在控制台上。

更新于:2021-09-21

759 次浏览

开启你的 事业

完成课程,获取认证

开始
广告