import itertools


def labeledBeamerFrames(pdfInfos):
    """Given a PDFInfos object, detect whether the PDF contains beamer
    \frame{}s with [label=name]s.  For every named frame, return a
    pair (name, list_of_pages) in a list.  If the PDF does not contain
    corresponding named link targets (with names like 'name',
    'name<1>', 'name<2>' etc.), returns an empty list."""
    result = []
    names = pdfInfos.names()
    for name in names:
        pages = []
        for subframe in itertools.count(1):
            page = names.get(name + b'<%d>' % subframe)
            if page is None:
                break
            pages.append(page)
        if pages:
            result.append((name, pages))
    print("%d pages out of %d belong to labeled beamer slides." % (
        sum(len(pages) for name, pages in result), pdfInfos.pageCount()))
    return sorted(result, key = lambda name_pages: name_pages[1][0])