计算用一个以上的键找到一个字典值的次数 [英] Count how many times a dictionary value is found with more than one key

查看:57
本文介绍了计算用一个以上的键找到一个字典值的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python.有没有一种方法可以计算使用一个以上的键在字典中找到值的次数,然后返回计数?

I'm working in python. Is there a way to count how many times values in a dictionary are found with more than one key, and then return a count?

因此,例如,如果我有50个值,并且运行了一个脚本来执行此操作,那么我将得到一个看起来像这样的计数:

So if for example I had 50 values and I ran a script to do this, I would get a count that would look something like this:

1: 23  
2: 15  
3: 7  
4: 5  

上面的内容告诉我,1个键中显示23个值,2个键中显示15个值,3个键中显示7个值,4个键中显示5个值.

The above would be telling me that 23 values appear in 1 key, 15 values appear in 2 keys, 7 values appear in 3 keys and 5 values appear in 4 keys.

此外,如果我的字典中每个键有多个值,这个问题是否会改变?

Also, would this question change if there were multiple values per key in my dictionary?

这是我字典的一个样本(它是细菌名称):

Here is a sample of my dictionary (it's bacteria names):

{'0': ['Pyrobaculum'], '1': ['Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium'], '3': ['Thermoanaerobacter', 'Thermoanaerobacter'], '2': ['Helicobacter', 'Mycobacterium'], '5': ['Thermoanaerobacter', 'Thermoanaerobacter'], '4': ['Helicobacter'], '7': ['Syntrophomonas'], '6': ['Gelria'], '9': ['Campylobacter', 'Campylobacter'], '8': ['Syntrophomonas'], '10': ['Desulfitobacterium', 'Mycobacterium']}

因此,从此示例中,有8个唯一值,我将获得理想的反馈:

So from this sample, there are 8 unique values, I the ideal feedback I would get be:

1:4
2:3
3:1

因此,一个键中只有4个细菌名称,两个键中有3个细菌,三个键中有1个细菌.

So 4 bacteria names are only in one key, 3 bacteria are found in two keys and 1 bacteria is found in three keys.

推荐答案

所以除非我读错了,否则你想知道:

So unless I'm reading this wrong you want to know:

  • 对于原始字典中的每个值,每个不同的值计数会出现多少次?
  • 本质上您想要的是频率 字典中的值

我采取了一种不太优雅的方法,但其他方法却无法解决,但已将问题分解为各个步骤:

I took a less elegant approach that the other answers, but have broken the problem down for you into individual steps:

d = {'0': ['Pyrobaculum'], '1': ['Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium'], '3': ['Thermoanaerobacter', 'Thermoanaerobacter'], '2': ['Helicobacter', 'Mycobacterium'], '5': ['Thermoanaerobacter', 'Thermoanaerobacter'], '4': ['Helicobacter'], '7': ['Syntrophomonas'], '6': ['Gelria'], '9': ['Campylobacter', 'Campylobacter'], '8': ['Syntrophomonas'], '10': ['Desulfitobacterium', 'Mycobacterium']}

# Iterate through and find out how many times each key occurs
vals = {}                       # A dictonary to store how often each value occurs.
for i in d.values():
  for j in set(i):              # Convert to a set to remove duplicates
    vals[j] = 1 + vals.get(j,0) # If we've seen this value iterate the count
                                # Otherwise we get the default of 0 and iterate it
print vals

# Iterate through each possible freqency and find how many values have that count.
counts = {}                     # A dictonary to store the final frequencies.
# We will iterate from 0 (which is a valid count) to the maximum count
for i in range(0,max(vals.values())+1):
    # Find all values that have the current frequency, count them
    #and add them to the frequency dictionary
    counts[i] = len([x for x in vals.values() if x == i])

for key in sorted(counts.keys()):
  if counts[key] > 0:
     print key,":",counts[key]

您还可以在键盘上测试此代码.

这篇关于计算用一个以上的键找到一个字典值的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆