python 拆分大集合成多个小集合
2021/8/1 1:05:53
本文主要是介绍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 拆分大集合成多个小集合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践