【问题标题】:How to place each element in a list as a list in another list?如何将列表中的每个元素作为另一个列表中的列表?
【发布时间】:2018-06-21 08:43:54
【问题描述】:

我有一个清单:

list1 = [1,2,3,4,5]

我想要输出:

list2 = [[1],[2],[3],[4],[5]]

我尝试过以各种方式遍历列表 1,但没有成功。

【问题讨论】:

标签: python python-3.x list


【解决方案1】:

也可以使用map():

>>> list1 = [1,2,3,4,5]
>>> list2 = list(map(lambda x: [x], list1))
>>> print(list2)
[[1], [2], [3], [4], [5]]

【讨论】:

    【解决方案2】:

    试试这个

    list2 = [[item] for item in list1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多