pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

跳過可迭代元素的優(yōu)雅方式

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

問題描述

我有一個(gè)大的迭代器,事實(shí)上,一個(gè)大的迭代器由以下給出:

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

itertools.permutations(range(10))

我想訪問第百萬個(gè)元素.我已經(jīng)用一些不同的方式解決了問題.

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

  1. 將可迭代對象轉(zhuǎn)換為列表并獲取第 1000000 個(gè)元素:

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

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

  • 手動(dòng)跳過元素直到 999999:

  • Manually skiping elements till 999999:

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

  • 手動(dòng)跳過元素 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 的優(yōu)雅方式.第一個(gè)選項(xiàng)太昂貴了,它需要計(jì)算整個(gè)迭代來訪問單個(gè)元素.如果我沒記錯(cuò)的話,islice 在內(nèi)部進(jìn)行的計(jì)算與我在方法 2 中所做的相同,并且?guī)缀跖c第 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 中是否有其他方式可以訪問可迭代的具體元素,或者至少以更優(yōu)雅的方式跳過第一個(gè)元素,或者我是否只需要使用上述之一.

    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() 調(diào)用;它使用 n, n,實(shí)際上不返回 anything,并且 next() 函數(shù)回退到默認(rèn)值.

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

    簡化為您的示例,您希望跳過 999999 個(gè)元素,然后返回元素 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 循環(huán)無法比擬的.

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

    為了說明,以下是每種方法僅重復(fù) 10 次的時(shí)間:

    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.

    這篇關(guān)于跳過可迭代元素的優(yōu)雅方式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

    相關(guān)文檔推薦

    How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
    How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
    How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
    How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
    Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
    Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
    主站蜘蛛池模板: 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 | 好看的韩国漫画_韩漫在线免费阅读-汗汗漫画 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 环比机械| 深圳公司注册-工商注册公司-千百顺代理记账公司| 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 低压载波电能表-单相导轨式电能表-华邦电力科技股份有限公司-智能物联网综合管理平台 | 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 背压阀|减压器|不锈钢减压器|减压阀|卫生级背压阀|单向阀|背压阀厂家-上海沃原自控阀门有限公司 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 小型UV打印机-UV平板打印机-大型uv打印机-UV打印机源头厂家 |松普集团 | 杭州ROHS检测仪-XRF测试仪价格-百科 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司| 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 电力测功机,电涡流测功机,磁粉制动器,南通远辰曳引机测试台 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 立刷【微电签pos机】-嘉联支付立刷运营中心 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 新车测评网_网罗汽车评测资讯_汽车评测门户报道 | 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 | 水轮机密封网 | 水轮机密封产品研发生产厂家 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 |