我正在处理一个问题,要求我在字典中返回最不频繁的值,除了几个不同的计数之外我似乎无法解决它,但是没有一定数量的值支票中提供的词典。
例如,假设字典包含从学生姓名(字符串)到其年龄(整数)的映射。您的方法将返回最不常见的年龄。考虑包含以下键/值对的字典变量d:
{'Alyssa':22,'Char':25,'Dan':25,'Jeff':20,'Kasey':20,'Kim':20,'Mogran':25,'Ryan': 25,'Stef':22}三个人年龄20岁(杰夫,凯西和金),两个人年龄22岁(Alyssa和Stef),四个人年龄25岁(Char,Dan,Mogran和Ryan)。最稀有的(d)返回22,因为那个年龄只有两个人。
有人会介意我指出正确的方向吗?谢谢!
答案 0 :(得分:8)
计算集合的成员是collections.Counter
:
d={'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22}
import collections
print collections.Counter(d.values()).most_common()[-1][0]
22
答案 1 :(得分:1)
你可以为计数器创建一个空的dict,然后遍历你得到的dict,并在第二个dict中的相应值中加1,然后返回第二个dict中具有最小值的元素的键。
答案 2 :(得分:1)
from collections import Counter
min(Counter(my_dict_of_ages.values()).items(),key=lambda x:x[1])
我会这样做
答案 3 :(得分:1)
您可以使用collections.Counter
d={'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22}
import collections
print collections.Counter(d.values()).most_common()[-1][0]
或编写自己的函数:
def rarest(dict):
values = dict.values()
least_frequent = max(values)
for x in set(values):
if values.count(x) < least_frequent:
least_frequent = x
return {least_frequent:dict[least_frequent]}
>>> rarest({'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22})
{22:2}
答案 4 :(得分:0)
您可以创建第二个字典,该字典使用第一个(年龄)中的值作为第二个中的键,第二个值作为计数。然后对第二个值进行排序并进行反向循环以获取关联的键(通过将键列表和值列表视为numpy数组,有几种方法可以有效地执行此操作。)
import numpy
d = {'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22}
def rarest(d):
s = {}
# First, map ages to counts.
for key in d:
if d[key] not in s:
s[d[key]] = 1
else:
s[d[key]] += 1 # Could use a defaultdict for this.
# Second, sort on the counts to find the rarest.
keys = numpy.array(s.keys())
values = numpy.array(s.values())
ordering = np.argsort(values)
return keys[ordering][0]
可能有more efficient way to do this,但这似乎有效。
答案 5 :(得分:0)
my_dict = {'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22}
values = my_dict.values()
most_frequent = 0
for x in set(values):
if values.count(x) > most_frequent:
most_frequent = x
print most_frequent
此代码使用set()
方法,该方法返回包含所有唯一元素的集合,即:
>> set([1, 2, 3, 4, 2, 1])
set([1, 2, 3, 4])
要从dict中提取所有值,可以使用dict.values()
。同样,您有dict.keys()
和dict.items()
。
>> my_dict.keys()
['Char', 'Stef', 'Kim', 'Jeff', 'Kasey', 'Dan', 'Mogran', 'Alyssa', 'Ryan']
>> my_dict.values()
[25, 22, 20, 20, 20, 25, 25, 22, 25]
>> my_dict.items()
[('Char', 25),
('Stef', 22),
('Kim', 20),
('Jeff', 20),
('Kasey', 20),
('Dan', 25),
('Mogran', 25),
('Alyssa', 22),
('Ryan', 25)]
答案 6 :(得分:0)
万一其他人希望记住尽可能少的功能/属性名称和程序包,JadedTuna的答案很好。这是我要做的:
val_count = {}
for k in d:
if k in val_count.keys():
val_count[k] += 1
else:
val_count[k] = 1
val_count = list(val_count.items()) # Convert dict to [(k1, v1), (k2, v2), ...]
val_count.sort(key=lambda tup: tup[1]) # Sorts by count. Add reverse=True if you'd like mode instead
val_count[0]