久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在python中每隔n個項目拆分一個生成器/可迭代(s

split a generator/iterable every n items in python (splitEvery)(在python中每隔n個項目拆分一個生成器/可迭代(splitEvery))
本文介紹了在python中每隔n個項目拆分一個生成器/可迭代(splitEvery)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試用 Python 編寫 Haskell 函數splitEvery".這是它的定義:

I'm trying to write the Haskell function 'splitEvery' in Python. Here is it's definition:

splitEvery :: Int -> [e] -> [[e]]
    @'splitEvery' n@ splits a list into length-n pieces.  The last
    piece will be shorter if  does not evenly divide the length of
    the list.

它的基本版本可以正常工作,但我想要一個可以與生成器表達式、列表和迭代器一起使用的版本.而且,如果有一個生成器作為輸入,它應該返回一個生成器作為輸出!

The basic version of this works fine, but I want a version that works with generator expressions, lists, and iterators. And, if there is a generator as an input it should return a generator as an output!

# should not enter infinite loop with generators or lists
splitEvery(itertools.count(), 10)
splitEvery(range(1000), 10)

# last piece must be shorter if n does not evenly divide
assert splitEvery(5, range(9)) == [[0, 1, 2, 3, 4], [5, 6, 7, 8]]

# should give same correct results with generators
tmp = itertools.islice(itertools.count(), 10)
assert list(splitEvery(5, tmp)) == [[0, 1, 2, 3, 4], [5, 6, 7, 8]]

當前實施

這是我目前擁有的代碼,但它不適用于簡單的列表.

Current Implementation

Here is the code I currently have but it doesn't work with a simple list.

def splitEvery_1(n, iterable):
    res = list(itertools.islice(iterable, n))
    while len(res) != 0:
        yield res
        res = list(itertools.islice(iterable, n))

這個不適用于生成器表達式(感謝 jellybean 修復它):

This one doesn't work with a generator expression (thanks to jellybean for fixing it):

def splitEvery_2(n, iterable): 
    return [iterable[i:i+n] for i in range(0, len(iterable), n)]

必須有一段簡單的代碼來進行拆分.我知道我可以擁有不同的功能,但它似乎應該很容易做到.我可能陷入了一個不重要的問題,但這真的讓我很煩惱.

There has to be a simple piece of code that does the splitting. I know I could just have different functions but it seems like it should be and easy thing to do. I'm probably getting stuck on an unimportant problem but it's really bugging me.

類似于 http://docs.python.org 的 grouper/library/itertools.html#itertools.groupby 但我不希望它填充額外的值.

It is similar to grouper from http://docs.python.org/library/itertools.html#itertools.groupby but I don't want it to fill extra values.

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

它確實提到了一種截斷最后一個值的方法.這也不是我想要的.

It does mention a method that truncates the last value. This isn't what I want either.

保證迭代的從左到右的評估順序.這使得使用 izip(*[iter(s)]*n) 將數據系列聚類為 n 長度組成為可能.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using izip(*[iter(s)]*n).

list(izip(*[iter(range(9))]*5)) == [[0, 1, 2, 3, 4]]
# should be [[0, 1, 2, 3, 4], [5, 6, 7, 8]]

推薦答案

from itertools import islice

def split_every(n, iterable):
    i = iter(iterable)
    piece = list(islice(i, n))
    while piece:
        yield piece
        piece = list(islice(i, n))

一些測試:

>>> list(split_every(5, range(9)))
[[0, 1, 2, 3, 4], [5, 6, 7, 8]]

>>> list(split_every(3, (x**2 for x in range(20))))
[[0, 1, 4], [9, 16, 25], [36, 49, 64], [81, 100, 121], [144, 169, 196], [225, 256, 289], [324, 361]]

>>> [''.join(s) for s in split_every(6, 'Hello world')]
['Hello ', 'world']

>>> list(split_every(100, []))
[]

這篇關于在python中每隔n個項目拆分一個生成器/可迭代(splitEvery)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲视频在线一区 | 亚洲精品国产综合区久久久久久久 | 成人在线观看网址 | 免费看一级毛片 | 欧美在线 | 亚洲视频一区 | 婷婷福利视频导航 | 国产精品日韩一区 | 在线欧美亚洲 | 精品在线观看一区二区 | 国产精品福利视频 | 丝袜 亚洲 欧美 日韩 综合 | 日韩视频一区二区三区 | 欧美日韩在线成人 | 中文字幕在线观看av | 超碰电影| 精品福利在线 | 欧美日韩免费一区二区三区 | 极品的亚洲 | 亚洲女人的天堂 | 五月婷婷 六月丁香 | 国产精品视频一区二区三区不卡 | 久久成人精品一区二区三区 | 99精品视频免费观看 | 国产精品久久一区二区三区 | 精品久久不卡 | 精品欧美一区二区三区久久久 | 日韩一区二区三区四区五区六区 | 久久青青 | 日韩视频在线观看中文字幕 | 一区二区三区视频在线观看 | 国产农村妇女毛片精品久久麻豆 | 黄网站在线播放 | 高清黄色网址 | 一区二区三区国产好的精 | 久久精品 | 久久精品视频在线免费观看 | 久久久久久久久久久久久91 | 亚洲成人免费av | 免费黄色录像片 | 天天干天天想 |