一只名叫tom的猫
该方法旨在分析排名靠前的所有商店。如果它们在任何其他排名列表中的位置不低于第一位,则它们属于该前级并被添加到“级别”列表中。接下来,将它们从领先者中删除,并调整所有列表,以便有新的领先者。重复这个过程,直到没有商店离开。def rank_stores(rankings): """ Rank stores with rankings by volume sales with over lap between lists. :param rankings: list of rankings of stores also in lists. :return: Ordered list with sets of items at same rankings. """ rank_global = [] # Evaluate all stores in the number one postion, if they are not below # number one somewhere else, then they belong at this level. # Then remove them from the front of the list, and repeat. while sum([len(x) for x in rankings]) > 0: tops = [] # Find out which of the number one stores are not in a lower position # somewhere else. for rank in rankings: if not rank: continue else: top = rank[0] add = True for rank_test in rankings: if not rank_test: continue elif not rank_test[1:]: continue elif top in rank_test[1:]: add = False break else: continue if add: tops.append(top) # Now add tops to total rankings list, # then go through the rankings and pop the top if in tops. rank_global.append(set(tops)) # Remove the stores that just made it to the top. for rank in rankings: if not rank: continue elif rank[0] in tops: rank.pop(0) else: continue return rank_global对于提供的排名:ranking_1 = ['J','A','Z','B','C']ranking_2 = ['A','H','K','B']ranking_3 = ['Q','O','A','N','K']rankings = [ranking_1, ranking_2, ranking_3]然后调用函数:rank_stores(rankings)结果是:[{'J', 'Q'}, {'O'}, {'A'}, {'H', 'N', 'Z'}, {'K'}, {'B'}, {'C'}]在某些情况下,可能没有足够的信息来确定明确的排名。试试这个顺序。['Z', 'A', 'B', 'J', 'K', 'F', 'L', 'E', 'W', 'X', 'Y', 'R', 'C']我们可以得出以下排名:a = ['Z', 'A', 'B', 'F', 'E', 'Y']b = ['Z', 'J', 'K', 'L', 'X', 'R']c = ['F', 'E', 'W', 'Y', 'C']d = ['J', 'K', 'E', 'W', 'X']e = ['K', 'F', 'W', 'R', 'C']f = ['X', 'Y', 'R', 'C']g = ['Z', 'F', 'W', 'X', 'Y', 'R', 'C']h = ['Z', 'A', 'E', 'W', 'C']i = ['L', 'E', 'Y', 'R', 'C']j = ['L', 'E', 'W', 'R']k = ['Z', 'B', 'K', 'L', 'W', 'Y', 'R']rankings = [a, b, c, d, e, f, g, h, i, j, k]调用函数:rank_stores(rankings)结果是:[{'Z'}, {'A', 'J'}, {'B'}, {'K'}, {'F', 'L'}, {'E'}, {'W'}, {'X'}, {'Y'}, {'R'}, {'C'}]在这种情况下,没有足够的信息来确定“J”相对于“A”和“B”的位置。只是它在“Z”和“K”之间的范围内。当在数百个排名和商店中相乘时,某些商店将无法按绝对数量正确排名。