如何将列表中的每个值放入其自己的列表中?
假设我们得到:
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]]
关于python - 为列表中的每个值生成嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58601758/