一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python如何将一个数组均分为多个数组 Python将一个数组均分为多个数组实例

时间:2019-11-07 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Python将一个数组均分为多个数组实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

将一个数组,均分为多个数组

代码

# -*- coding:utf-8 -*-
# py3

def list_split(items, n):
  return [items[i:i+n] for i in range(0, len(items), n)]

if '__main__' == __name__:
  list1 = ['s1', 's2', 's3', 's4', 's5', 's6', 's7']
  list2 = list_split(list1, 3)
  print(list2)

输出

[['s1', 's2', 's3'], ['s4', 's5', 's6'], ['s7']]

热门栏目