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

跳過可迭代元素的優雅方式

Elegant way to skip elements in an iterable(跳過可迭代元素的優雅方式)
本文介紹了跳過可迭代元素的優雅方式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個大的迭代器,事實上,一個大的迭代器由以下給出:

I've got a large iterable, in fact, a large iterable given by:

itertools.permutations(range(10))

我想訪問第百萬個元素.我已經用一些不同的方式解決了問題.

I would like to access to the millionth element. I alredy have problem solved in some different ways.

  1. 將可迭代對象轉換為列表并獲取第 1000000 個元素:

  1. Casting iterable to list and getting 1000000th element:

return list(permutations(range(10)))[999999]

  • 手動跳過元素直到 999999:

  • Manually skiping elements till 999999:

    p = permutations(range(10))
    for i in xrange(999999): p.next()
    return p.next()
    

  • 手動跳過元素 v2:

  • Manually skiping elements v2:

    p = permutations(range(10))
    for i, element in enumerate(p):
        if i == 999999:
            return element
    

  • 使用 itertools 中的 islice:

  • Using islice from itertools:

    return islice(permutations(range(10)), 999999, 1000000).next()
    

  • 但我仍然不覺得它們都不是 python 的優雅方式.第一個選項太昂貴了,它需要計算整個迭代來訪問單個元素.如果我沒記錯的話,islice 在內部進行的計算與我在方法 2 中所做的相同,并且幾乎與第 3 次一樣,也許它有更多的冗余操作.

    But I still don't feel like none of them is the python's elegant way to do that. First option is just too expensive, it needs to compute the whole iterable just to access a single element. If I'm not wrong, islice does internally the same computation I just did in method 2, and is almost exactly as 3rd, maybe it has even more redundant operations.

    所以,我只是好奇,想知道在 python 中是否有其他方式可以訪問可迭代的具體元素,或者至少以更優雅的方式跳過第一個元素,或者我是否只需要使用上述之一.

    So, I'm just curious, wondering if there is in python some other way to access to a concrete element of an iterable, or at least to skip the first elements, in some more elegant way, or if I just need to use one of the aboves.

    推薦答案

    使用itertools 配方 consume 跳過 n 元素:

    def consume(iterator, n):
        "Advance the iterator n-steps ahead. If n is none, consume entirely."
        # Use functions that consume iterators at C speed.
        if n is None:
            # feed the entire iterator into a zero-length deque
            collections.deque(iterator, maxlen=0)
        else:
            # advance to the empty slice starting at position n
            next(islice(iterator, n, n), None)
    

    注意那里的 islice() 調用;它使用 n, n,實際上不返回 anything,并且 next() 函數回退到默認值.

    Note the islice() call there; it uses n, n, effectively not returning anything, and the next() function falls back to the default.

    簡化為您的示例,您希望跳過 999999 個元素,然后返回元素 1000000:

    Simplified to your example, where you want to skip 999999 elements, then return element 1000000:

    return next(islice(permutations(range(10)), 999999, 1000000))
    

    islice() 在 C 中處理迭代器,這是 Python 循環無法比擬的.

    islice() processes the iterator in C, something that Python loops cannot beat.

    為了說明,以下是每種方法僅重復 10 次的時間:

    To illustrate, here are the timings for just 10 repeats of each method:

    >>> from itertools import islice, permutations
    >>> from timeit import timeit
    >>> def list_index():
    ...     return list(permutations(range(10)))[999999]
    ... 
    >>> def for_loop():
    ...     p = permutations(range(10))
    ...     for i in xrange(999999): p.next()
    ...     return p.next()
    ... 
    >>> def enumerate_loop():
    ...     p = permutations(range(10))
    ...     for i, element in enumerate(p):
    ...         if i == 999999:
    ...             return element
    ... 
    >>> def islice_next():
    ...     return next(islice(permutations(range(10)), 999999, 1000000))
    ... 
    >>> timeit('f()', 'from __main__ import list_index as f', number=10)
    5.550895929336548
    >>> timeit('f()', 'from __main__ import for_loop as f', number=10)
    1.6166789531707764
    >>> timeit('f()', 'from __main__ import enumerate_loop as f', number=10)
    1.2498459815979004
    >>> timeit('f()', 'from __main__ import islice_next as f', number=10)
    0.18969106674194336
    

    islice() 方法比第二快的方法快近 7 倍.

    The islice() method is nearly 7 times faster than the next fastest method.

    這篇關于跳過可迭代元素的優雅方式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
    主站蜘蛛池模板: 国产精品中文字幕在线观看 | 亚洲激情一级片 | 91佛爷在线观看 | 日韩精品一区二区三区老鸭窝 | 欧美一级免费观看 | 天天天操 | av在线免费观看网站 | 亚洲一区二区三区在线播放 | 久久综合久久久 | 九九热精品视频 | 亚洲成人av在线 | 日韩精品中文字幕在线 | 亚洲精视频| 欧美日韩一区二区三区四区 | 日韩一区二区免费视频 | 国产免费一区二区 | 国产九九九 | 精品一二区| 男女羞羞网站 | a国产视频 | www国产精品 | 日韩在线观看中文字幕 | 精品不卡| 久久伊人在| 五月天国产视频 | 亚洲精品久久久久久一区二区 | 国产成人99久久亚洲综合精品 | 中文字字幕一区二区三区四区五区 | 国产一区二区三区高清 | 综合二区 | 日韩一二区 | a视频在线播放 | 久久精品男人的天堂 | 99pao成人国产永久免费视频 | 在线看av网址 | 欧美久久久久久久久中文字幕 | 夜夜夜夜草 | 看片wwwwwwwwwww | 色久伊人 | 日韩中文字幕一区二区 | 欧美在线日韩 |