'Combine and sort multiple sets of recs.
Combines multiple sets of recs and sorts by number of times each rec
appears, the reasoning being that an item recommended for several of
the original srs is more likely to match the "theme" of the set.'

@classmethod
def _merge_and_sort_by_count(cls, rows):

    rank_id36_pairs = chain.from_iterable((row._values().iteritems() for row in rows))
    ranks = defaultdict(list)
    for (rank, id36) in rank_id36_pairs:
        ranks[id36].append(rank)
    recs = [(id36, len(ranks), max(ranks)) for (id36, ranks) in ranks.iteritems()]
    recs = sorted(recs, key=itemgetter(2))
    return sorted(recs, key=itemgetter(1), reverse=True)
