Python實現(xiàn)將列表拆分為大小為N的塊
方法1:使用yield
yield關(guān)鍵字使函數(shù)能夠在再次調(diào)用時返回到它停止的位置。這是與常規(guī)函數(shù)的關(guān)鍵區(qū)別,一個常規(guī)的函數(shù)不能回到它停止的地方。yield關(guān)鍵字幫助函數(shù)記住其狀態(tài),yield使函數(shù)能夠掛起和恢復(fù),同時它在掛起執(zhí)行時返回一個值。
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)
在這個例子中,我們使用了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中,將列表拆分為一行代碼,將列表拆分為多個列表是一種優(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]]
另一種實現(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個大小相等的塊。
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ù)迭代列表,每次生成一個塊。當(dāng)deque為空時,循環(huán)中斷,這表明所有元素都已被處理。
方法7: 部分賦值
這里有一個例子,你可以輕松地處理大小為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實現(xiàn)將列表拆分為大小為N的塊的文章就介紹到這了,更多相關(guān)Python拆分列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實現(xiàn)基于神經(jīng)網(wǎng)絡(luò)的圖像風(fēng)格遷移功能
圖像風(fēng)格遷移是深度學(xué)習(xí)領(lǐng)域的一個經(jīng)典應(yīng)用,它能夠?qū)⒁粡垐D片的藝術(shù)風(fēng)格應(yīng)用到另一張圖片上,創(chuàng)造出令人驚艷的藝術(shù)效果,本項目將帶你從零開始構(gòu)建一個完整的全棧Web應(yīng)用,實現(xiàn)基于神經(jīng)網(wǎng)絡(luò)的圖像風(fēng)格遷移功能,需要的朋友可以參考下2025-11-11
ubuntu17.4下為python和python3裝上pip的方法
今天小編就為大家分享一篇ubuntu17.4下為python和python3裝上pip的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
圖解Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別
這篇文章主要介紹了Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別,淺拷貝和深拷貝想必大家在學(xué)習(xí)中遇到很多次,這也是面試中常常被問到的問題,本文就帶你詳細(xì)了解一下2023-05-05
Python中的偏函數(shù)及其廣泛應(yīng)用方式
這篇文章主要介紹了Python中的偏函數(shù)及其廣泛應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

