欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)將列表拆分為大小為N的塊

 更新時(shí)間:2023年09月07日 09:40:48   作者:python收藏家  
這篇文章主要為大家整理了一些常見的Python實(shí)現(xiàn)將列表拆分為大小為N的塊的方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,有需要的小伙伴可以了解下

方法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)示例

    本文主要介紹了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ù)詳解

    今天小編就為大家分享一篇對(duì)Tensorflow中的矩陣運(yùn)算函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python3調(diào)用百度AI識(shí)別圖片中的文字功能示例【測(cè)試可用】

    Python3調(diào)用百度AI識(shí)別圖片中的文字功能示例【測(cè)試可用】

    這篇文章主要介紹了Python3調(diào)用百度AI識(shí)別圖片中的文字功能,結(jié)合實(shí)例形式分析了Python3安裝及使用百度AI接口的相關(guān)操作技巧,并附帶說明了百度官方AI平臺(tái)的注冊(cè)及接口調(diào)用操作方法,需要的朋友可以參考下
    2019-03-03
  • 詳解Python列表賦值復(fù)制深拷貝及5種淺拷貝

    詳解Python列表賦值復(fù)制深拷貝及5種淺拷貝

    這篇文章主要介紹了Python列表賦值,復(fù)制,深拷貝以及5種淺拷貝詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Python多進(jìn)程同步簡(jiǎn)單實(shí)現(xiàn)代碼

    Python多進(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-04
  • 用Python獲取亞馬遜商品信息

    用Python獲取亞馬遜商品信息

    大家好,本篇文章主要講的是用Python獲取亞馬遜商品信息,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Python中的lambda和apply用法及說明

    Python中的lambda和apply用法及說明

    這篇文章主要介紹了Python中的lambda和apply用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python的GUI編程之Pack、Place、Grid的區(qū)別說明

    Python的GUI編程之Pack、Place、Grid的區(qū)別說明

    這篇文章主要介紹了Python的GUI編程之Pack、Place、Grid的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 基于tensorflow權(quán)重文件的解讀

    基于tensorflow權(quán)重文件的解讀

    這篇文章主要介紹了關(guān)于tensorflow權(quán)重文件的解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2021-05-05
  • pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵

    pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵

    這篇文章主要介紹了pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評(píng)論