Joining a set of ordered-integer yielding Python iterators

后端 未结 7 2338
囚心锁ツ
囚心锁ツ 2020-12-13 05:33

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 th

7条回答
  •  孤城傲影
    2020-12-13 06:03

    def postings(posts):
        sets = (set(l) for l in posts)
        return sorted(reduce(set.intersection, sets))
    

    ... you could try and take advantage of the fact that the lists are ordered, but since reduce, generator expressions and set are all implemented in C, you'll probably have a hard time doing better than the above with logic implemented in python.

提交回复
热议问题