首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何连接一组有序整数生成Python迭代器?

如何连接一组有序整数生成Python迭代器?
EN

Stack Overflow用户
提问于 2018-01-31 07:37:42
回答 2查看 0关注 0票数 0

:

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

想得到这样的输出:

[100, 322]
EN

回答 2

Stack Overflow用户

发布于 2018-01-31 16:28:05

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]
票数 0
EN

Stack Overflow用户

发布于 2018-01-31 17:21:19

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

在python中实现的逻辑可能会比上面的要好一些。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档