其他分享
首页 > 其他分享> > 擅长使用iter

擅长使用iter

作者:互联网

def populate_ranks(votes, ranks):
    names = list(votes.keys())
    names.sort(key=votes.get, reverse=True)
    for i, name in enumerate(names, 1):
        ranks[name] = i


def get_winner(ranks):
    return next(iter(ranks))


if __name__ == '__main__':
    ranks = {}
    votes = {
        'other': 1281,
        'polar bear': 587,
        'fox': 863,
    }
    populate_ranks(votes, ranks)
    print(ranks)
    winner = get_winner(ranks)
    print(winner)

标签:__,votes,get,ranks,winner,iter,擅长,names,使用
来源: https://www.cnblogs.com/weiweivip666/p/16410780.html