Python實(shí)現(xiàn)將列表拆分為大小為N的塊
方法1:使用yield
yield關(guān)鍵字使函數(shù)能夠在再次調(diào)用時(shí)返回到它停止的位置。這是與常規(guī)函數(shù)的關(guān)鍵區(qū)別,一個(gè)常規(guī)的函數(shù)不能回到它停止的地方。yield關(guān)鍵字幫助函數(shù)記住其狀態(tài),yield使函數(shù)能夠掛起和恢復(fù),同時(shí)它在掛起執(zhí)行時(shí)返回一個(gè)值。
my_list = ['geeks', 'for', 'geeks', 'like', 'geeky','nerdy', 'geek', 'love', 'questions','words', 'life'] # Yield successive n-sized # chunks from l. def divide_chunks(l, n): # looping till length l for i in range(0, len(l), n): yield l[i:i + n] # How many elements each # list should have n = 5 x = list(divide_chunks(my_list, n)) print (x)
輸出
[['geeks', 'for', 'geeks', 'like', 'geeky'],
['nerdy', 'geek', 'love', 'questions', 'words'],
['life']]
方法2:使用for循環(huán)
在這個(gè)例子中,我們使用了Python中的循環(huán)和列表切片,這將幫助我們將列表分成塊。
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] start = 0 end = len(my_list) step = 3 for i in range(start, end, step): x = i print(my_list[x:x+step])
輸出
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
方法3: 使用列表解析
在Python中,將列表拆分為一行代碼,將列表拆分為多個(gè)列表是一種優(yōu)雅的方式。
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # How many elements each # list should have n = 4 # using list comprehension final = [my_list[i * n:(i + 1) * n] for i in range((len(my_list) + n - 1) // n )] print (final)
輸出
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
另一種實(shí)現(xiàn)方式:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9] # How many elements each # list should have n = 4 # using list comprehension x = [l[i:i + n] for i in range(0, len(l), n)] print(x)
輸出
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
方法4:使用Numpy
在這里,我們使用Numpy.array_split,它將數(shù)組拆分為n個(gè)大小相等的塊。
import numpy as np arr = range(30) np.array_split(arr, 6)
輸出
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9]), array([10, 11, 12, 13, 14]), array([15, 16, 17, 18, 19]), array([20, 21, 22, 23, 24]), array([25, 26, 27, 28, 29])]
方法5:使用itertools
from itertools import islice def chunk(arr_range, arr_size): arr_range = iter(arr_range) return iter(lambda: tuple(islice(arr_range, arr_size)), ()) print(list(chunk(range(30), 5)))
輸出
[(0, 1, 2, 3, 4),
(5, 6, 7, 8, 9),
(10, 11, 12, 13, 14),
(15, 16, 17, 18, 19),
(20, 21, 22, 23, 24),
(25, 26, 27, 28, 29)]
方法6: 使用collections
from collections import deque def split_list(input_list, chunk_size): # Create a deque object from the input list deque_obj = deque(input_list) # While the deque object is not empty while deque_obj: # Pop chunk_size elements from the left side of the deque object # and append them to the chunk list chunk = [] for _ in range(chunk_size): if deque_obj: chunk.append(deque_obj.popleft()) # Yield the chunk yield chunk input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] chunk_size = 3 chunks = list(split_list(input_list, chunk_size)) print(chunks)
輸出
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
deque類允許您輕松地從列表的左側(cè)或右側(cè)移除元素,從而輕松地將列表分割為特定大小的塊。代碼使用while循環(huán)和生成器函數(shù)迭代列表,每次生成一個(gè)塊。當(dāng)deque為空時(shí),循環(huán)中斷,這表明所有元素都已被處理。
方法7: 部分賦值
這里有一個(gè)例子,你可以輕松地處理大小為N的塊列表:
my_list = list(range(10)) chunk_size = 3 while my_list: chunk, my_list = my_list[:chunk_size], my_list[chunk_size:] print(chunk)
輸出
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]
到此這篇關(guān)于Python實(shí)現(xiàn)將列表拆分為大小為N的塊的文章就介紹到這了,更多相關(guān)Python拆分列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jmeter執(zhí)行python腳本的實(shí)現(xiàn)示例
本文主要介紹了jmeter執(zhí)行python腳本的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05對(duì)Tensorflow中的矩陣運(yùn)算函數(shù)詳解
今天小編就為大家分享一篇對(duì)Tensorflow中的矩陣運(yùn)算函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python3調(diào)用百度AI識(shí)別圖片中的文字功能示例【測(cè)試可用】
這篇文章主要介紹了Python3調(diào)用百度AI識(shí)別圖片中的文字功能,結(jié)合實(shí)例形式分析了Python3安裝及使用百度AI接口的相關(guān)操作技巧,并附帶說明了百度官方AI平臺(tái)的注冊(cè)及接口調(diào)用操作方法,需要的朋友可以參考下2019-03-03Python多進(jìn)程同步簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Python多進(jìn)程同步簡(jiǎn)單實(shí)現(xiàn)代碼,涉及Python基于Process與Lock模塊運(yùn)行進(jìn)程與鎖機(jī)制實(shí)現(xiàn)多進(jìn)程同步的相關(guān)技巧,需要的朋友可以參考下2016-04-04Python的GUI編程之Pack、Place、Grid的區(qū)別說明
這篇文章主要介紹了Python的GUI編程之Pack、Place、Grid的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵
這篇文章主要介紹了pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01