如何将列表中的集合转换为python中的列表? [英] How to cast set in list into list in python?

查看:176
本文介绍了如何将列表中的集合转换为python中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将列表中的集合强制转换为如下所示。

I want to cast set in list to list like below.

before: [(1, 1, 1), (1, 1, 0), (1, 0, 1)]
after: [[1, 1, 1], [1, 1, 0], [1, 0, 1]]

我需要尽可能简单的代码。

I need the as simple code as possible.

推荐答案

>>> x = [(1, 1, 1), (1, 1, 0), (1, 0, 1)]
>>> list(map(list, x))
[[1, 1, 1], [1, 1, 0], [1, 0, 1]]



说明



map(list,x)接受一个可迭代的 x 并将函数 list 应用于此可迭代的每个元素。因此,元组(1,1,1)成为列表 [1,1,1] (1、1、0)变为 [1、1、0] (1、0, 1)变为 [1、0、1]

Explanation

map(list, x) takes an iterable x and applies function list to each element of this iterable. Thus the tuple (1, 1, 1) becomes the list [1, 1, 1], (1, 1, 0) becomes [1, 1, 0] and (1, 0, 1) becomes [1, 0, 1].

存储在 map 对象中(假设Python 3.x)。 map 对象是一个迭代器,可以通过在其上调用 list 将其转换为列表,如上所示。但是,通常不需要进行显式转换,因为迭代器允许您直接遍历元素:

These lists are then stored in a map object (assuming Python 3.x). A map object is an iterator, which can be converted to a list by calling list on it, as shown above. Often, though, you don't need to make this explicit conversion because iterator allows you to traverse the elements directly:

>>> for elem in map(list, x):
...     print(elem)
...
[1, 1, 1]
[1, 1, 0]
[1, 0, 1]

这篇关于如何将列表中的集合转换为python中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆