Skip to main content
 首页 » 编程设计

python之为列表中的每个值生成嵌套列表

2024年11月01日8powertoolsteam

如何将列表中的每个值放入其自己的列表中?

假设我们得到:

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

我怀疑 for 或 while 循环可以解决问题:

for number in Numbers: 
    Numbers.append([number]) 

但这似乎不能满足我想做的事情。

请您参考如下方法:

尝试使用列表理解:

print([[i] for i in Numbers]) 

或者使用 map :

print(list(map(lambda x: [x], Numbers))) 

输出:

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