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