尚方宝剑之说
max 只为最高价值对于您的结果,您不需要显式映射,例如通过字典。您可以计算最高值的索引,然后将其应用于您的密钥列表:lista = ["a", "b", "c", "d"]listb = [80, 90, 70, 60]# a couple of alternatives to extract index with maximum valueidx = max(range(len(listb)), key=lambda x: listb[x]) # 1idx, _ = max(enumerate(listb), key=lambda x: x[1]) # 1maxkey = lista[idx] # 'b'heapq最高n值如果您想要最高的n 个值,则不需要完全排序。您可以使用heapq.nlargest:from heapq import nlargestfrom operator import itemgettern = 2# a couple of alternatives to extract index with n highest valuesidx = nlargest(n, range(len(listb)), key=lambda x: listb[x]) # [1, 0]idx, _ = zip(*nlargest(n, enumerate(listb), key=lambda x: x[1])) # (1, 0)maxkeys = itemgetter(*idx)(lista) # ('b', 'a')
萧十郎
keys = ['a', 'b', 'c', 'd']values = [80, 90, 70, 60]dictionary = dict(zip(keys, values))print(dictionary){'a': 80, 'b': 90, 'c': 70, 'd': 60}我想你可以尝试使用 operator.itemgetter:import operatormax(dictionary.iteritems(), key=operator.itemgetter(1))[0]告诉我这是否有效