Python以固定長度分割數組list


 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()

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM