Python编写了一个按奖牌计数排序的程序。将获得奖牌数量最多的三个国家/地区保存到“ top_three”列表中

时间:2019-03-06 08:30:18

标签: python python-3.x

我的代码是:

add_filter( 'oembed_response_data', 'disable_embeds_filter_oembed_response_data_' );
function disable_embeds_filter_oembed_response_data_( $data ) {
    unset($data['author_url']);
    unset($data['author_name']);
    return $data;
}

实际结果:

medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
def g(k,d):
    return d[k]
ks = medals.keys()
top_three = sorted(ks,key=lambda x : g(x,medals),reverse = True)

但想要得到结果

['United States','China','Russia','Germany','Japan','South Korea']

怎么可能,请帮助任何人。

3 个答案:

答案 0 :(得分:0)

medals = {'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}
def g(k,d):
    return d[k]
ks = medals.keys()
top_three = sorted(ks,key=lambda x : g(x,medals),reverse = True)[:3]

答案 1 :(得分:0)

只需将三个项目的分隔符添加到排序结果中即可:

top_three = sorted(ks,key=lambda x : g(x,medals),reverse = True)[:3]

答案 2 :(得分:0)

required = list(dict(sorted(medals.items(), key = lambda x: x[1],reverse= True)).keys())[:3]

other_approach =list(map(lambda x: x[0],sorted(medals.items(), key = lambda x: x[1],reverse= True)))[:3]