Python 切分数组,将一个数组均匀切分成多个数组


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']]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM