1 def split_list_by_n(list_collection, n): 2 """ 3 将集合均分,每份n个元素 4 :param list_collection: 5 :param n: 6 :return:返回的结果为评分后的每份可迭代对象 7 """ 8 for i in range(0, len(list_collection), n): 9 yield list_collection[i: i + n] 10 11 def main(): 12 # time.sleep(1) 13 list_temp = [1, 2, 3, 4, 5, 6, 7, 8, 9] 14 temp = split_list_by_n(list_temp, 4) 15 for i in temp: 16 print(i) 17 18 if __name__ == '__main__': 19 main()
结果: