weixin_46025415
weixin_46025415
采纳率72.7%
2020-10-25 10:51

列表切割问题怎么解决?

road = ['01', '04', '05', '02']

请问我想输出

new_road=[['01', '04'], ['04', '05'], [ '05', '02']]

请问应该怎么办?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • c513881038 sscsdl 6月前
    var oldArr = ['01', '04', '05', '02'];
    var newArr = [];
    for (i=0;i<oldArr.length;i++) {
      if (i < oldArr.length-1) newArr[i] = [oldArr[i], oldArr[i+1]]
    }
    
    点赞 2 评论 复制链接分享
  • qq_43656607 放风喽 6月前
    road = ['01', '04', '05', '02']
    new_road = []
    for i in range(len(road)-1):
            new_road.append([road[i],road[i+1]])
    print(new_road)
    
    点赞 2 评论 复制链接分享

相关推荐