python 拆分大集合成多个小集合

使用python 将大的list 拆分成多个小的list:

def toChildList(bigList=[], count=None):  # 一个大集合,count=要拆分的每个小集合里边的元素数量
    listCollection = []
    if len(bigList) < int(count):
        listCollection.append(bigList)
        return listCollection

    length = len(bigList)

    flag = True
    startIndex = 0
    endIndex = count
    while flag:
        if startIndex < len(bigList):
            listCollection.append(bigList[startIndex:endIndex])
            startIndex = startIndex + count
            endIndex = endIndex + count
        else:
            flag = False

    return listCollection


if __name__ == '__main__':
    biglist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
    print("biglist length :", len(biglist))

    childLists = toChildList(biglist, 5)

    print("childLists: ", childLists)
    print("childLists length: ", len(childLists))

结果:
python 拆分大集合成多个小集合

上一篇:python项目工具里封装(mysql、sendmail、requests)


下一篇:JS实现文字替换