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

加入一組產生 Python 迭代器的有序整數

Joining a set of ordered-integer yielding Python iterators(加入一組產生 Python 迭代器的有序整數)
本文介紹了加入一組產生 Python 迭代器的有序整數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是一個看似簡單的問題:給定一個生成升序整數序列的迭代器列表,編寫一個只生成每個序列中出現的整數的簡潔生成器.

Here is a seemingly simple problem: given a list of iterators that yield sequences of integers in ascending order, write a concise generator that yields only the integers that appear in every sequence.

昨晚閱讀了幾篇論文后,我決定用 Python 編寫一個完全最小的全文索引器,在這里看到(雖然那個版本現在很老了).

After reading a few papers last night, I decided to hack up a completely minimal full text indexer in Python, as seen here (though that version is quite old now).

我的問題在于 search() 函數,它必須遍歷每個發布列表并僅生成每個列表中出現的文檔 ID.從上面的鏈接可以看出,我當前的非遞歸工作"嘗試很糟糕.

My problem is with the search() function, which must iterate over each posting list and yield only the document IDs that appear on every list. As you can see from the link above, my current non-recursive 'working' attempt is terrible.

示例:

postings = [[1,   100, 142, 322, 12312],
            [2,   100, 101, 322, 1221],
            [100, 142, 322, 956, 1222]]

應該讓步:

[100, 322]

至少有一個優雅的遞歸函數解決方案,但如果可能的話,我想避免這種情況.但是,涉及嵌套生成器表達式、itertools 濫用或任何其他類型的代碼高爾夫的解決方案非常受歡迎.:-)

There is at least one elegant recursive function solution to this, but I'd like to avoid that if possible. However, a solution involving nested generator expressions, itertools abuse, or any other kind of code golf is more than welcome. :-)

應該可以將函數安排為只需要與最小列表中的項目一樣多的步驟,而無需將整組整數吸入內存.將來,這些列表可能會從磁盤中讀取,并且大于可用的 RAM.

It should be possible to arrange for the function to only require as many steps as there are items in the smallest list, and without sucking the entire set of integers into memory. In future, these lists may be read from disk, and larger than available RAM.

在過去的 30 分鐘里,我在舌尖有了一個想法,但我無法將其完全融入代碼中.請記住,這只是為了好玩!

For the past 30 minutes I've had an idea on the tip of my tongue, but I can't quite get it into code. Remember, this is just for fun!

推薦答案

import heapq, itertools
def intersect(*its):
    for key, values in itertools.groupby(heapq.merge(*its)):
        if len(list(values)) == len(its):
            yield key

>>> list(intersect(*postings))
[100, 322]

這篇關于加入一組產生 Python 迭代器的有序整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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的天堂 | 欧美一级免费黄色片 | 亚洲色图婷婷 | 美女福利视频网站 | 在线观看精品 | 特黄色一级毛片 | 91亚洲国产成人久久精品网站 | 国内精品久久久久久久影视简单 | 亚洲看片| 国产精品国产a | 国产福利91精品 | 四虎成人在线播放 | 涩涩视频在线观看 | 欧美自拍一区 | 亚洲国产精品久久 | 久草福利| 国产一区久久 | 91精品国产高清一区二区三区 | 欧美黄页| 欧美在线观看黄色 | 日韩一区中文字幕 | 国产精品色 | 天天精品在线 | 欧美一级高清片 | 精品国产第一区二区三区 | 翔田千里一区二区 | 国产亚洲精品精品国产亚洲综合 | 户外露出一区二区三区 | 国产欧美在线播放 | 91在线视频国产 | com.国产 | 久久久精品一区二区三区 | 久久99一区二区 | 欧美激情网站 | 精品一二三 | 日韩欧美天堂 | 久久久久国产成人精品亚洲午夜 | 国产一区二区三区四区在线观看 | 日韩欧美亚洲综合 | 国产亚洲精品美女久久久久久久久久 | 韩日一区二区 |