Python程序在字典中查找最高的3个值
在本文中,我们将学习解决给定问题陈述的解决方案和方法。
问题陈述
给定字典,我们需要找到三个最高值并显示它们。
方法1-使用收集模块(计数器功能)
示例
from collections import Counter
# Initial Dictionary
my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21}
k = Counter(my_dict)
# Finding 3 highest values
high = k.most_common(3)
print("Dictionary with 3 highest values:")
print("Keys: Values")
for i in high:
print(i[0]," :",i[1]," ")输出结果
Dictionary with 3 highest values: Keys: Values r : 21 t : 6 o : 5
方法2-使用heapq模块(最大功能)
示例
from collections import Counter
# Initial Dictionary
my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21}
k = Counter(my_dict)
# Finding 3 highest values
high = k.most_common(3)
print("Dictionary with 3 highest values:")
print("Keys: Values")
for i in high:
print(i[0]," :",i[1]," ")输出结果
Dictionary with 3 highest values: Keys: Values r : 21 t : 6 o : 5
结论
在本文中,我们了解了将十进制数转换为二进制数的方法。
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬