首页 > 解决方案 > 如何将平面列表转换为嵌套列表,其中包含原始列表中的每一对?

问题描述

我怎样才能打开一个列表,例如:

og_list = [0, 4, 12, 18, 20, 23]

到嵌套列表中,例如:

new_list = [[0, 4], [4, 12], [12, 18], [18, 20], [20, 23]]

我怎样才能做到这一点?

标签: pythonlistnested

解决方案


new_list = []
for i in range(len(og_list)-1):
  new_list.append( [og_list[i],og_list[i+1]] )

推荐阅读